Skip to main content
For cases when your audio file is not publicly accessible, you can upload it to the pyannoteAI servers using the Media APIs. This tutorial shows you how to upload your audio files to a temporary storage location and use it to diarize your audio.
Some notes on using the Media APIs:
  • Temporary: The audio files you upload are only temporarily stored and will be automatically removed within 48 hours.
  • Secure: The media is stored in an isolated location that only you can access with your API token. It is encrypted while in transit with HTTPS. Access to the production system is controlled to prevent unauthorized use.

1. Create a storage location and get a pre-signed PUT URL

First, you need to create a temporary storage location by sending a value for the url parameter. This endpoint returns a pre-signed PUT URL that you can use to upload your audio file. The url should be in the form media://object-key where the object-key can be any alpha-numeric string you choose to identify your file. The object-key is unique to your team, meaning no other team can access files stored under your object keys. For example:
  • media://my-audio-file-123
  • media://meetings/2024-01-15/call.wav
  • media://customer_conversation_20240115
You’ll use this key to refer to your uploaded file in subsequent API requests (e.g. diarization).
uploadAudio.ts
import axios from 'axios';
import fs from 'fs';

async function uploadAudioFile(inputPath: string, objectKey: string, apiKey: string): Promise<void> {
  try {
    // Create the pre-signed PUT URL
    const response = await axios.post(
      'https://api.pyannote.ai/v1/media/input',
      { url: `media://${objectKey}` }, // Replace with your desired object-key
      {
        headers: {
          'Authorization': `Bearer ${apiKey}`, // In production, use environment variables: process.env.PYANNOTE_API_KEY
          'Content-Type': 'application/json'
        }
      }
    );

    const presignedUrl = response.data.url;

    // Upload local file to the pre-signed URL
    console.log(`Uploading ${inputPath} to ${presignedUrl}`);
    
    const fileData = fs.readFileSync(inputPath);
    await axios.put(presignedUrl, fileData, {
      headers: {
        'Content-Type': 'application/octet-stream'
      }
    });

    console.log('File uploaded successfully!');
  } catch (error) {
    console.error('Error uploading file:', error);
    throw error;
  }
}

// Usage
// uploadAudioFile('./path/to/your/audio.wav', 'my-meeting-recording', 'YOUR_API_KEY');

2. Diarize your uploaded audio file

After you have uploaded your audio file, you can use it in e.g. a diarization job by providing the media key you created earlier as URL in the input.
createJob.ts
import axios from 'axios';

async function createDiarizationJob(objectKey: string, apiKey: string) {
  try {
    const body = {
      url: `media://${objectKey}` // Use the same object-key you used when creating the pre-signed URL
    };

    const response = await axios.post(
      'https://api.pyannote.ai/v1/diarize',
      body,
      {
        headers: {
          'Authorization': `Bearer ${apiKey}`, // In production, use environment variables: process.env.PYANNOTE_API_KEY
          'Content-Type': 'application/json'
        }
      }
    );

    console.log(response.data);
    return response.data;
  } catch (error) {
    console.error('Error creating diarization job:', error);
    throw error;
  }
}

// Usage
// createDiarizationJob('my-meeting-recording', 'YOUR_API_KEY');
Great! You have successfully uploaded your audio file and used it to create a diarization job. See the diarization tutorial for more details on how to process the diarization results.

API Reference