# Upload an asset

> An upload link for one file; PUT the bytes to it.

<Endpoint method="POST" path="/v1/assets/uploads" />

<CodeTabs>

```bash tab="cURL"
curl https://api.muzekit.com/v1/assets/uploads \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "name": "logo.png", "type": "image/png", "brand": true }'

curl -X PUT "$UPLOAD_URL" -H "Content-Type: image/png" --data-binary @logo.png
```

```javascript tab="Node"
import { readFile } from "node:fs/promises";

const res = await fetch("https://api.muzekit.com/v1/assets/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ name: "logo.png", type: "image/png", brand: true }),
});
const upload = await res.json();

await fetch(upload.upload_url, {
  method: "PUT",
  headers: { "Content-Type": "image/png" },
  body: await readFile("logo.png"),
});
```

```python tab="Python"
import os
import requests

# json= sends the body with Content-Type: application/json
upload = requests.post(
    "https://api.muzekit.com/v1/assets/uploads",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={"name": "logo.png", "type": "image/png", "brand": True},
).json()

with open("logo.png", "rb") as f:
    requests.put(upload["upload_url"], headers={"Content-Type": "image/png"}, data=f)
```

</CodeTabs>

| Field | |
|---|---|
| `name`, `type` | The file name and MIME type |
| `size` | Optional; refused early when over the limit |
| `kind` | Optional: `image`, `video`, `audio`, `font` (read from `type` otherwise) |
| `family` | Fonts: the family name in the font list |
| `user` | Upload into this end user's library |
| `brand` | Add to the editor's [brand kit](/docs/brand-kit) (not with `user`) |

The link works once, for 15 minutes. The PUT answers with the file's `id`, `url`, `width` and `height`; Muzekit reads the type from the bytes and refuses files that don't match, are over the limit, or are unsafe SVGs.

Without `user` or `brand`, the file goes into the workspace library. Uploading the same file again to the same library gives back the asset it already has.

## Examples

### Upload into an end user's library

Send `user` to put the file in that end user's own uploads. They see it every time they open the editor, and other users don't.

<CodeTabs>

```bash tab="cURL"
UPLOAD_URL=$(curl -s https://api.muzekit.com/v1/assets/uploads \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "name": "photo.jpg", "type": "image/jpeg", "user": "usr_8fK2mQ1xZpR" }' \
  | jq -r .upload_url)

curl -X PUT "$UPLOAD_URL" -H "Content-Type: image/jpeg" --data-binary @photo.jpg
```

```javascript tab="Node"
import { readFile } from "node:fs/promises";

const res = await fetch("https://api.muzekit.com/v1/assets/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "photo.jpg",
    type: "image/jpeg",
    user: "usr_8fK2mQ1xZpR",
  }),
});
const upload = await res.json();

const put = await fetch(upload.upload_url, {
  method: "PUT",
  headers: { "Content-Type": "image/jpeg" },
  body: await readFile("photo.jpg"),
});
const asset = await put.json(); // asset.id is the new ast_… id
```

```python tab="Python"
import os
import requests

upload = requests.post(
    "https://api.muzekit.com/v1/assets/uploads",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={"name": "photo.jpg", "type": "image/jpeg", "user": "usr_8fK2mQ1xZpR"},
).json()

with open("photo.jpg", "rb") as f:
    asset = requests.put(
        upload["upload_url"], headers={"Content-Type": "image/jpeg"}, data=f
    ).json()
```

</CodeTabs>

The PUT answers with the stored file:

```json
{
  "url": "https://storage.muzekit.com/…/photo.jpg",
  "type": "image/jpeg",
  "size": 482113,
  "width": 1600,
  "height": 1200,
  "id": "ast_dQC3Z8RgqAH"
}
```

### Add a font to the brand kit

Send `"brand": true` and the `family` name the font list should show. Upload one file per weight, each with the same `family`.

<CodeTabs>

```bash tab="cURL"
UPLOAD_URL=$(curl -s https://api.muzekit.com/v1/assets/uploads \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Acme-Bold.woff2",
    "type": "font/woff2",
    "family": "Acme",
    "brand": true
  }' \
  | jq -r .upload_url)

curl -X PUT "$UPLOAD_URL" -H "Content-Type: font/woff2" --data-binary @Acme-Bold.woff2
```

```javascript tab="Node"
import { readFile } from "node:fs/promises";

const res = await fetch("https://api.muzekit.com/v1/assets/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "Acme-Bold.woff2",
    type: "font/woff2",
    family: "Acme",
    brand: true,
  }),
});
const upload = await res.json();

await fetch(upload.upload_url, {
  method: "PUT",
  headers: { "Content-Type": "font/woff2" },
  body: await readFile("Acme-Bold.woff2"),
});
```

```python tab="Python"
import os
import requests

upload = requests.post(
    "https://api.muzekit.com/v1/assets/uploads",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={
        "name": "Acme-Bold.woff2",
        "type": "font/woff2",
        "family": "Acme",
        "brand": True,
    },
).json()

with open("Acme-Bold.woff2", "rb") as f:
    requests.put(upload["upload_url"], headers={"Content-Type": "font/woff2"}, data=f)
```

</CodeTabs>

### Upload a video

Videos are uploaded the same way. Send `size` too, so a file over the video limit (`413 file_too_large`) or over your plan's storage (`402 storage_full`) is refused before you send the bytes.

<CodeTabs>

```bash tab="cURL"
SIZE=$(wc -c < promo.mp4 | tr -d ' ')

UPLOAD_URL=$(curl -s https://api.muzekit.com/v1/assets/uploads \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d "{ \"name\": \"promo.mp4\", \"type\": \"video/mp4\", \"size\": $SIZE }" \
  | jq -r .upload_url)

curl -X PUT "$UPLOAD_URL" -H "Content-Type: video/mp4" --data-binary @promo.mp4
```

```javascript tab="Node"
import { readFile, stat } from "node:fs/promises";

const res = await fetch("https://api.muzekit.com/v1/assets/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "promo.mp4",
    type: "video/mp4",
    size: (await stat("promo.mp4")).size,
  }),
});
const upload = await res.json();
if (!res.ok) throw new Error(upload.error.message);

await fetch(upload.upload_url, {
  method: "PUT",
  headers: { "Content-Type": "video/mp4" },
  body: await readFile("promo.mp4"),
});
```

```python tab="Python"
import os
import requests

res = requests.post(
    "https://api.muzekit.com/v1/assets/uploads",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={
        "name": "promo.mp4",
        "type": "video/mp4",
        "size": os.path.getsize("promo.mp4"),
    },
)
res.raise_for_status()
upload = res.json()

with open("promo.mp4", "rb") as f:
    requests.put(upload["upload_url"], headers={"Content-Type": "video/mp4"}, data=f)
```

</CodeTabs>

### Copy a file from your own storage

When the file already lives at a URL (your bucket or CDN), download it on your server and PUT the bytes. Muzekit keeps its own copy, so the editor never loads your URL.

<CodeTabs>

```bash tab="cURL"
UPLOAD_URL=$(curl -s https://api.muzekit.com/v1/assets/uploads \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "name": "banner.png", "type": "image/png", "user": "usr_8fK2mQ1xZpR" }' \
  | jq -r .upload_url)

curl -s https://cdn.example.com/banner.png \
  | curl -X PUT "$UPLOAD_URL" -H "Content-Type: image/png" --data-binary @-
```

```javascript tab="Node"
const res = await fetch("https://api.muzekit.com/v1/assets/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "banner.png",
    type: "image/png",
    user: "usr_8fK2mQ1xZpR",
  }),
});
const upload = await res.json();

const file = await fetch("https://cdn.example.com/banner.png");
await fetch(upload.upload_url, {
  method: "PUT",
  headers: { "Content-Type": "image/png" },
  body: Buffer.from(await file.arrayBuffer()),
});
```

```python tab="Python"
import os
import requests

upload = requests.post(
    "https://api.muzekit.com/v1/assets/uploads",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={"name": "banner.png", "type": "image/png", "user": "usr_8fK2mQ1xZpR"},
).json()

file = requests.get("https://cdn.example.com/banner.png")
requests.put(
    upload["upload_url"], headers={"Content-Type": "image/png"}, data=file.content
)
```

</CodeTabs>

## Response

```json
{
  "object": "upload",
  "upload_url": "https://api.muzekit.com/runtime/uploads/…",
  "url": "https://storage.muzekit.com/…/logo.png",
  "kind": "image",
  "expires_in": 900
}
```
