# Get an upload link for a file

Step 1 of posting a file from your computer. POST /uploads.

`POST https://api.tiktornado.com/api/v1/uploads`

Scope: `posts:write`.

Step 1 of posting a file from your computer. The answer gives an `uploadUrl` and a `mediaRef`.

1. Upload the file with an HTTP `PUT` to `uploadUrl`, sending only the headers in the answer's `headers`, for example: `curl -X PUT -H 'Content-Type: video/mp4' --upload-file clip.mp4 '<uploadUrl>'`.
2. Pass `mediaRef` in a post's `mediaRefs`, or as `settings.coverImageRef`.

`mediaRef` is an opaque value: store it and pass it back exactly as you got it. Ask for one link per file. The link expires, so upload soon after you get it, and post the file soon after you upload it.

Videos: MP4, MOV or WebM, up to 4 GB. Photos: JPEG, PNG or WebP, up to 20 MB. TikTok takes videos from 3 seconds to 10 minutes long. The length isn't checked when you post, only when the post goes out: a video outside that range fails then with `media_rejected`.

This route needs no `Idempotency-Key`: a repeat simply gives you a new link.

## Request body

- `accountId` string, required: An id, letters and digits only. Matches `^[A-Za-z0-9]{1,64}$`.
- `filename` string, required: The file's name, for example `clip.mp4`. 1 to 255 characters.
- `contentType` string, required: The file's type (`file_type_not_supported` for any other). One of `video/mp4`, `video/quicktime`, `video/webm`, `image/jpeg`, `image/png`, `image/webp`.
- `size` integer, required: The file's size in bytes. Videos up to 4 GB (`file_too_large`), photos up to 20 MB (`photo_too_large`). 1 to 4,000,000,000.

## Examples

### A video

```bash
curl -X POST 'https://api.tiktornado.com/api/v1/uploads' \
  -H "Authorization: Bearer $TIKTORNADO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "accountId": "j971x2c4ajrq0w8sd5e8fxz3a57v2k8m",
    "filename": "leg-day.mp4",
    "contentType": "video/mp4",
    "size": 48213504
  }'
```

```js
const response = await fetch("https://api.tiktornado.com/api/v1/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.TIKTORNADO_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "accountId": "j971x2c4ajrq0w8sd5e8fxz3a57v2k8m",
    "filename": "leg-day.mp4",
    "contentType": "video/mp4",
    "size": 48213504
  }),
});
const data = await response.json();
```

```python
import os

import requests

response = requests.post(
    "https://api.tiktornado.com/api/v1/uploads",
    headers={
        "Authorization": f"Bearer {os.environ['TIKTORNADO_API_KEY']}",
    },
    json={
        "accountId": "j971x2c4ajrq0w8sd5e8fxz3a57v2k8m",
        "filename": "leg-day.mp4",
        "contentType": "video/mp4",
        "size": 48213504,
    },
)
data = response.json()
```

## Response 201

The upload link.

- `uploadUrl` string, required: Where to upload the file, with an HTTP `PUT`. For this file only. It expires. A URL.
- `mediaRef` string, required: An opaque value for the uploaded file. Pass it back exactly as you got it, in `mediaRefs` or as `settings.coverImageRef`.
- `method` string, required: Always `PUT`.
- `headers` object, required: The only headers to send with the upload.
  - `Content-Type` string, required

```json
{
  "uploadUrl": "https://uploads.example.com/put/leg-day.mp4?signature=EXAMPLE",
  "mediaRef": "https://media.example.com/uploads/7f3a9c/leg-day.mp4",
  "method": "PUT",
  "headers": {
    "Content-Type": "video/mp4"
  }
}
```

## Errors

| Status | Reason | Message |
| --- | --- | --- |
| 400 | `invalid_request` | Something here isn't valid. Check it and try again. |
| 400 | `file_type_not_supported` | This file type isn't supported. Use MP4, MOV or WebM for videos, or JPEG, PNG or WebP for photos. |
| 400 | `file_too_large` | This file is too big. Videos can be up to 4 GB and photos 20 MB. |
| 400 | `photo_too_large` | This photo is too big. Photos can be up to 20 MB. |
| 400 | `account_not_ready` | This account isn't ready to post yet. Or: This account is under review by TikTok, so new posts can't be scheduled right now. |
| 400 | `invalid_state` | This can't be done right now. Refresh and try again. |
| 401 | `api_key_invalid` | This API key isn't valid. Check it, or create a new one in the app. |
| 403 | `access_locked` | Your subscription isn't active, so this can't be changed right now. Visit Billing to fix it. |
| 403 | `api_scope_missing` | This API key can't do this. Create a key with the access it needs in the app. |
| 404 | `not_found` | We couldn't find this. |
| 429 | `slow_down` | You're going a bit fast. Wait a moment and try again. |
| 500 | `unknown_error` | Something went wrong on our side. We're on it. |
| 503 | `service_unavailable` | Something went wrong on our side. We're on it. |