> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pyannote.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Upload audio files

> This tutorial shows how to upload your files to a temporary location to run jobs like diarization

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.

<Note>
  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.
</Note>

## 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).

<CodeGroup dropdown>
  ```typescript uploadAudio.ts theme={null}
  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');
  ```

  ```bash theme={null}
  # Set your API key as an environment variable in production
  # export PYANNOTE_API_KEY="your_api_key_here"

  # Define your media object key
  OBJECT_KEY="my-meeting-recording"

  # Create pre-signed PUT URL
  curl -X POST "https://api.pyannote.ai/v1/media/input" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d "{\"url\": \"media://${OBJECT_KEY}\"}"

  # Upload file to the returned pre-signed URL
  curl -X PUT "PRESIGNED_URL_FROM_RESPONSE" \
    -H "Content-Type: application/octet-stream" \
    --data-binary "@/path/to/your/audio.wav"
  ```

  ```python upload_audio.py theme={null}
  import requests

  # Define your media object key
  object_key = "my-meeting-recording"  # Replace with your desired object-key

  # Create the pre-signed PUT URL.
  api_key = "YOUR_API_KEY"  # In production, use environment variables: os.getenv("PYANNOTE_API_KEY")
  response = requests.post(
      "https://api.pyannote.ai/v1/media/input",
      json={"url": f"media://{object_key}"},
      headers={
          "Authorization": f"Bearer {api_key}",
          "Content-Type": "application/json"
      }
  )
  response.raise_for_status()
  data = response.json()
  presigned_url = data["url"]

  # Upload local file to the pre-signed URL.
  print("Uploading {0} to {1}".format(input_path, presigned_url))
  with open(input_path, "rb") as input_file:
      # Upload your local audio file.
      requests.put(presigned_url, data=input_file)
  ```
</CodeGroup>

## 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.

<CodeGroup dropdown>
  ```typescript createJob.ts theme={null}
  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');
  ```

  ```bash theme={null}
  # Set your API key as an environment variable in production
  # export PYANNOTE_API_KEY="your_api_key_here"

  # Define your media object key (same as used in upload)
  OBJECT_KEY="my-meeting-recording"

  curl -X POST "https://api.pyannote.ai/v1/diarize" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d "{\"url\": \"media://${OBJECT_KEY}\"}"
  ```

  ```python create_job.py theme={null}
  import requests

  # Define your media object key (same as used in upload)
  object_key = "my-meeting-recording"  # Use the same object-key you used when creating the pre-signed URL

  # Replace the input value with your temporary storage location.
  body = {
    "url" : f"media://{object_key}",
  }

  url = "https://api.pyannote.ai/v1/diarize"
  api_key = "YOUR_API_KEY"  # In production, use environment variables: os.getenv("PYANNOTE_API_KEY")
  headers = {
     "Authorization": f"Bearer {api_key}",
     "Content-Type": "application/json"
  }

  response = requests.post(url, json=body, headers=headers)
  response.raise_for_status()
  print(response.json())
  ```
</CodeGroup>

Great! You have successfully uploaded your audio file and used it to create a diarization job.

See the [diarization tutorial](/tutorials/how-to-diarize-audio) for more details on how to process the diarization results.

## API Reference

* [Upload files](/api-reference/upload-media)
* [Get a job](/api-reference/get-job)
* [Diarize an audio file](/api-reference/diarize)
