> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/portkey-AI/gateway/llms.txt
> Use this file to discover all available pages before exploring further.

# Upload File

> Upload a file for use with fine-tuning or other operations

## POST /v1/files

Upload a file that can be used across various endpoints. The file size limit is 512 MB.

## Authentication

Requires provider authentication headers:

```bash theme={null}
x-portkey-provider: openai
Authorization: Bearer YOUR_OPENAI_API_KEY
```

## Request

### Headers

<ParamField header="x-portkey-provider" type="string" required>
  The provider to route the request to (e.g., `openai`, `azure-openai`)
</ParamField>

<ParamField header="Authorization" type="string" required>
  Bearer token for the provider API
</ParamField>

<ParamField header="x-portkey-config" type="string">
  Optional config for routing, retries, fallbacks
</ParamField>

### Body (multipart/form-data)

<ParamField body="file" type="file" required>
  The file to upload. Supported formats depend on the provider.
</ParamField>

<ParamField body="purpose" type="string" required>
  The intended purpose of the uploaded file. Common values:

  * `fine-tune` - For fine-tuning training data
  * `assistants` - For use with assistants
  * `batch` - For batch API requests
</ParamField>

## Response

<ResponseField name="id" type="string">
  The file identifier
</ResponseField>

<ResponseField name="object" type="string">
  The object type, always "file"
</ResponseField>

<ResponseField name="bytes" type="integer">
  The size of the file in bytes
</ResponseField>

<ResponseField name="created_at" type="integer">
  Unix timestamp of when the file was created
</ResponseField>

<ResponseField name="filename" type="string">
  The name of the file
</ResponseField>

<ResponseField name="purpose" type="string">
  The purpose of the file
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={null}
  curl https://localhost:8787/v1/files \
    -H "x-portkey-provider: openai" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -F purpose="fine-tune" \
    -F file="@training_data.jsonl"
  ```

  ```python Python theme={null}
  from portkey_ai import Portkey

  client = Portkey(
      provider="openai",
      Authorization="sk-***"
  )

  with open("training_data.jsonl", "rb") as file:
      response = client.files.create(
          file=file,
          purpose="fine-tune"
      )

  print(response)
  ```

  ```javascript JavaScript theme={null}
  import Portkey from 'portkey-ai';
  import fs from 'fs';

  const client = new Portkey({
      provider: "openai",
      Authorization: "sk-***"
  });

  const response = await client.files.create({
      file: fs.createReadStream("training_data.jsonl"),
      purpose: "fine-tune"
  });

  console.log(response);
  ```
</CodeGroup>

### Response Example

```json theme={null}
{
  "id": "file-abc123",
  "object": "file",
  "bytes": 140,
  "created_at": 1613779121,
  "filename": "training_data.jsonl",
  "purpose": "fine-tune"
}
```

## Error Handling

<ResponseField name="error" type="object">
  Error information if the request fails

  <Expandable title="error object">
    <ResponseField name="message" type="string">
      Human-readable error description
    </ResponseField>

    <ResponseField name="type" type="string">
      Error type (e.g., "invalid\_request\_error")
    </ResponseField>

    <ResponseField name="code" type="string">
      Error code
    </ResponseField>
  </Expandable>
</ResponseField>

## Best Practices

<Note>
  Files are processed asynchronously. For large files, use the retrieve endpoint to check the file status before using it.
</Note>

<Warning>
  Files have a maximum size limit of 512 MB. Larger files will be rejected.
</Warning>

## Related Endpoints

<CardGroup cols={2}>
  <Card title="List Files" icon="list" href="/api/files/list">
    View all uploaded files
  </Card>

  <Card title="Retrieve File" icon="file" href="/api/files/retrieve">
    Get file metadata
  </Card>

  <Card title="Delete File" icon="trash" href="/api/files/delete">
    Delete an uploaded file
  </Card>
</CardGroup>
