# Create or update a user

> Create a user ahead of their first session, or update one, matched by your own id.

<Endpoint method="POST" path="/v1/users" />

## Body

<ParamTable params={[
  { name: "id", type: "string", required: true, description: "Your own id for this user (1 to 255 characters). Stored as external_id." },
  { name: "name", type: "string", description: "Display name." },
  { name: "email", type: "string", description: "Email. Muzekit never emails end users." },
  { name: "locale", type: "string", description: "Editor language, e.g. en, de." },
  { name: "metadata", type: "object", description: "Your own data, up to 4 KB as JSON; more answers 400 invalid_request." }
]} />

## Request

<CodeTabs>

```bash tab="cURL"
curl https://api.muzekit.com/v1/users \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "id": "user_42", "name": "Ada", "email": "ada@example.com" }'
```

```javascript tab="Node"
const res = await fetch("https://api.muzekit.com/v1/users", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ id: "user_42", name: "Ada", email: "ada@example.com" }),
});
const user = await res.json();
```

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

# json= sends the body with Content-Type: application/json
user = requests.post(
    "https://api.muzekit.com/v1/users",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={"id": "user_42", "name": "Ada", "email": "ada@example.com"},
).json()
```

</CodeTabs>

The user is matched by `id` in this key's environment. A new id creates the user (and sends the `user.created` [webhook](/docs/webhooks)); a known id updates the fields you send and keeps the others. Both answer `201` with the user. You don't need this call before a session: [Create a session](/docs/api-reference/sessions-create) upserts the user the same way.

## Examples

### Add a user when they sign up in your app

Call it from your signup handler so the user exists before they first open the editor. You can then upload files into their library or list them in your admin before any session. Keep the returned `usr_…` id next to your own user.

<CodeTabs>

```bash tab="cURL"
curl https://api.muzekit.com/v1/users \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: signup-user_42" \
  -d '{
    "id": "user_42",
    "name": "Ada Lovelace",
    "email": "ada@example.com",
    "locale": "en",
    "metadata": { "tier": "pro", "company": "Analytical Engines" }
  }'
```

```javascript tab="Node"
// In your signup handler, after you create your own user
const res = await fetch("https://api.muzekit.com/v1/users", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": `signup-${user.id}`,
  },
  body: JSON.stringify({
    id: user.id,
    name: user.name,
    email: user.email,
    locale: user.locale,
    metadata: { tier: user.tier },
  }),
});
const { id: muzekitUserId } = await res.json(); // "usr_8fK2mQ1xZpR"
```

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

# In your signup handler, after you create your own user
res = requests.post(
    "https://api.muzekit.com/v1/users",
    headers={
        "Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}",
        "Idempotency-Key": f"signup-{user.id}",
    },
    json={
        "id": user.id,
        "name": user.name,
        "email": user.email,
        "locale": user.locale,
        "metadata": {"tier": user.tier},
    },
)
muzekit_user_id = res.json()["id"]  # "usr_8fK2mQ1xZpR"
```

</CodeTabs>

`metadata` is kept as you send it, for your own use. Muzekit doesn't read it, so it doesn't change what the editor allows. Set per-tier limits on the [session](/docs/api-reference/sessions-create#give-each-pricing-tier-its-own-editor) instead.

### Update a user when their profile changes

Send only the fields that changed. Fields you leave out keep their value. `metadata` is replaced as a whole, so send the full object, not just the changed key.

<CodeTabs>

```bash tab="cURL"
curl https://api.muzekit.com/v1/users \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "id": "user_42", "locale": "de", "metadata": { "tier": "team" } }'
```

```javascript tab="Node"
const res = await fetch("https://api.muzekit.com/v1/users", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ id: "user_42", locale: "de", metadata: { tier: "team" } }),
});
const user = await res.json();
```

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

user = requests.post(
    "https://api.muzekit.com/v1/users",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    json={"id": "user_42", "locale": "de", "metadata": {"tier": "team"}},
).json()
```

</CodeTabs>

The new `locale` sets the editor's language from the user's next session, unless that session passes its own `locale`.

### Import your existing users

Loop over your users once to add them all, for example before you launch the editor. Calls are rate limited to 600 a minute from one address, so a large import should pace itself.

<CodeTabs>

```bash tab="cURL"
# users.csv: id,name,email (no header row)
while IFS=, read -r id name email; do
  curl -s https://api.muzekit.com/v1/users \
    -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
    -H "Content-Type: application/json" \
    -d "{ \"id\": \"$id\", \"name\": \"$name\", \"email\": \"$email\" }"
  sleep 0.2
done < users.csv
```

```javascript tab="Node"
for (const u of await db.users.findAll()) {
  const res = await fetch("https://api.muzekit.com/v1/users", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ id: String(u.id), name: u.name, email: u.email }),
  });
  if (!res.ok) console.error(u.id, (await res.json()).error.code);
  await new Promise((r) => setTimeout(r, 200)); // stay under the rate limit
}
```

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

for u in db.users.all():
    res = requests.post(
        "https://api.muzekit.com/v1/users",
        headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
        json={"id": str(u.id), "name": u.name, "email": u.email},
    )
    if not res.ok:
        print(u.id, res.json()["error"]["code"])
    time.sleep(0.2)  # stay under the rate limit
```

</CodeTabs>

`id` must be a string, so convert numeric ids. A bad email answers `400 invalid_request` with `param: "email"` for that user only.

### Find a user's Muzekit id from your own id

Post just the `id` to get the user's `usr_…` id, then read what they made with [List templates](/docs/api-reference/templates-list) (`?user=usr_…`). If the id is new, this creates the user, so use ids from your own user table.

<CodeTabs>

```bash tab="cURL"
USER=$(curl -s https://api.muzekit.com/v1/users \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "id": "user_42" }' | jq -r .id)

curl "https://api.muzekit.com/v1/templates?user=$USER" \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY"
```

```javascript tab="Node"
const headers = {
  Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}`,
  "Content-Type": "application/json",
};
const user = await fetch("https://api.muzekit.com/v1/users", {
  method: "POST",
  headers,
  body: JSON.stringify({ id: "user_42" }),
}).then((r) => r.json());

const designs = await fetch(
  `https://api.muzekit.com/v1/templates?user=${user.id}`,
  { headers },
).then((r) => r.json());
```

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

headers = {"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"}
user = requests.post(
    "https://api.muzekit.com/v1/users",
    headers=headers,
    json={"id": "user_42"},
).json()

designs = requests.get(
    "https://api.muzekit.com/v1/templates",
    params={"user": user["id"]},
    headers=headers,
).json()
```

</CodeTabs>

Each call also moves the user's `last_active_at` to now. To avoid that, keep the `usr_…` id from the first response.

## Response

```json
{
  "object": "user",
  "id": "usr_8fK2mQ1xZpR",
  "external_id": "user_42",
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "locale": "en",
  "metadata": { "tier": "pro", "company": "Analytical Engines" },
  "last_active_at": "2026-10-16T21:35:00.000Z",
  "created_at": "2026-10-16T21:35:00.000Z"
}
```

Errors: `400 invalid_request` names the field in `param` (a missing `id`, a bad `email`, `metadata` over 4 KB). See [Errors](/docs/errors).
