# Create an API key

`POST https://my.neuronai.uz/api/v1/api-keys`

Справочник API › API ключи

Creates an API key and returns its `secret`, which is shown only this once.

A key cannot get more than the key that creates it: no higher permissions, no later expiry, no higher spend limit and no wider IP allowlist. A key made this way keeps working after the key that made it is revoked.

**Authentication:** `Authorization: Bearer <API key>`, a key with `api_keys:write`.

## Тело запроса (`application/json`)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | Yes | At least 1 character. At most 80 characters. |
| `description` | string \| null | No | At most 500 characters. |
| `access` | string | Yes | How much a key may do. A `full` key holds every product at its highest level, products added later included, but never `api_keys`, which is only ever granted explicitly. A `restricted` key holds exactly the levels in its `permissions` map. One of `full`, `restricted`. |
| `permissions` | object | No | Levels by product, such as `{"tts": "write"}`. On a `restricted` key a product left out is `none`. A `full` key already holds every other product at its highest level, so its map may name only `api_keys`. |
| `permissions.tts` | string | No | One of `none`, `read`, `write`. |
| `permissions.voices` | string | No | One of `none`, `read`, `write`. |
| `permissions.stt` | string | No | One of `none`, `write`. |
| `permissions.llm` | string | No | One of `none`, `read`, `write`. |
| `permissions.embeddings` | string | No | One of `none`, `write`. |
| `permissions.rerank` | string | No | One of `none`, `write`. |
| `permissions.account` | string | No | One of `none`, `read`. |
| `permissions.api_keys` | string | No | One of `none`, `read`, `write`. |
| `expires_at` | string \| null | No |  |
| `monthly_spend_limit` | number \| null | No | In credits per calendar month (UTC), from 0 to 999999999999.99 with at most two decimals. Leave it out, or send `null`, for no limit. |
| `allowed_ips` | string[] | No |  |

## Ответ (201, `application/json`)

Created.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `data` | object | Yes |  |
| `data.id` | string | Yes | A lowercase ULID. |
| `data.name` | string | Yes |  |
| `data.description` | string \| null | Yes |  |
| `data.masked_key` | string | Yes | `nai_...` and the last four characters of the secret. |
| `data.access` | string | Yes | How much a key may do. A `full` key holds every product at its highest level, products added later included, but never `api_keys`, which is only ever granted explicitly. A `restricted` key holds exactly the levels in its `permissions` map. One of `full`, `restricted`. |
| `data.permissions` | object | Yes | The effective level for every product. A full key reads as what it can do; `api_keys` is only ever granted explicitly. |
| `data.permissions.tts` | string | Yes | One of `none`, `read`, `write`. |
| `data.permissions.voices` | string | Yes | One of `none`, `read`, `write`. |
| `data.permissions.stt` | string | Yes | One of `none`, `write`. |
| `data.permissions.llm` | string | Yes | One of `none`, `read`, `write`. |
| `data.permissions.embeddings` | string | Yes | One of `none`, `write`. |
| `data.permissions.rerank` | string | Yes | One of `none`, `write`. |
| `data.permissions.account` | string | Yes | One of `none`, `read`. |
| `data.permissions.api_keys` | string | Yes | One of `none`, `read`, `write`. |
| `data.expires_at` | string \| null | Yes | When the key stops working, or `null` for never. |
| `data.allowed_ips` | string[] | Yes | Addresses or CIDR ranges the key may be used from. Empty allows any address. |
| `data.monthly_spend_limit` | number \| null | Yes | Credits per calendar month (UTC), or `null` for no limit. |
| `data.spent_this_month` | number | Yes | Credits spent this calendar month (UTC). |
| `data.enabled` | boolean | Yes |  |
| `data.revoked_at` | string \| null | Yes |  |
| `data.last_used_at` | string \| null | Yes |  |
| `data.created_at` | string | Yes |  |
| `data.secret` | string | No | The whole key. Only in the answer to a create, and never again. |
| `request_id` | string | Yes |  |

```json
{
  "data": {
    "id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41",
    "name": "Aziza",
    "description": "string",
    "masked_key": "nai_••••••••3f9a",
    "access": "full",
    "permissions": {
      "tts": "none",
      "voices": "none",
      "stt": "none",
      "llm": "none",
      "embeddings": "none",
      "rerank": "none",
      "account": "none",
      "api_keys": "none"
    },
    "expires_at": "2026-11-17T09:30:00Z",
    "allowed_ips": [
      "203.0.113.10"
    ],
    "monthly_spend_limit": 500,
    "spent_this_month": 42.5,
    "enabled": true,
    "revoked_at": "2026-11-17T09:30:00Z",
    "last_used_at": "2026-11-17T09:30:00Z",
    "created_at": "2026-11-17T09:30:00Z",
    "secret": "nai_8xK2pQ7vR4tY9mW3nZ5b3f9a"
  },
  "request_id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41"
}
```

### Заголовки

| Header | Type | Description |
| --- | --- | --- |
| `Cache-Control` | string | Includes `no-store`: the answer holds the new key's secret, so no cache may keep a copy. |
| `X-Request-Id` | string | Identifies this request. A JSON body carries it as `request_id` too; quote it to support. |
| `X-RateLimit-Limit` | integer | The requests a minute the rate limit on this endpoint allows. Sent once the limit has counted the request, so not when the key itself is refused. |
| `X-RateLimit-Remaining` | integer | How many of them are left this minute. |

## Ошибки

Every error has the same JSON shape:

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_request",
    "message": "The request is malformed or failed validation.",
    "param": null
  },
  "request_id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41"
}
```

| Status | Meaning |
| --- | --- |
| 401 | The API key is missing or not recognized (`invalid_api_key`), revoked (`api_key_revoked`), disabled (`api_key_disabled`) or expired (`api_key_expired`). |
| 403 | The key lacks the permission (`insufficient_permissions`), the IP is not allowed (`ip_not_allowed`), or the new key would exceed the calling key's own access or limits (`insufficient_permissions`). |
| 422 | The request failed validation (`invalid_request`). `error.fields` names each invalid field. |
| 429 | A rate or concurrency limit was reached (`rate_limit_exceeded`, `concurrency_limit_exceeded`). Retry after `Retry-After` seconds. |
| default | Any other error, in the same shape, such as `server_error` (500) or `service_unavailable` (503). |

## Examples

### cURL

```bash
# Needs a key holding api_keys:write.
curl -X POST "https://my.neuronai.uz/api/v1/api-keys" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <your-api-key>" \
  -d '{
    "name": "CI",
    "access": "restricted",
    "permissions": {"tts": "write"},
    "expires_at": "2030-01-01T00:00:00Z"
  }'

# The response's data.secret is shown only this once — store it now.
```

### TypeScript SDK

```typescript
import {NeuronAI} from "@naiuz/sdk";

const client = new NeuronAI();

// Needs a key holding api_keys:write.
const key = await client.apiKeys.create({
    name: "CI",
    access: "restricted",
    permissions: {tts: "write"},
    expires_at: "2030-01-01T00:00:00Z",
});

// The secret is shown only this once: store it now.
console.log(key.secret);
```

### Python SDK

```python
from naiuz import NeuronAI

client = NeuronAI()

# Needs a key holding api_keys:write.
key = client.api_keys.create(
    name="CI",
    access="restricted",
    permissions={"tts": "write"},
    expires_at="2030-01-01T00:00:00Z",
)

# The secret is shown only this once: store it now.
print(key.secret)
```

### PHP SDK

```php
$client = new \Naiuz\NeuronAI();

// Needs a key holding api_keys:write.
$key = $client->apiKeys->create([
    'name' => 'CI',
    'access' => 'restricted',
    'permissions' => ['tts' => 'write'],
    'expires_at' => '2030-01-01T00:00:00Z',
]);

// The secret is shown only this once: store it now.
echo $key->secret, PHP_EOL;
```

### JavaScript

```javascript
const response = await fetch('https://my.neuronai.uz/api/v1/api-keys', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer <your-api-key>',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        name: 'CI',
        access: 'restricted',
        permissions: { tts: 'write' },
        expires_at: '2030-01-01T00:00:00Z',
    }),
});

const { data } = await response.json();

// Shown only this once — store it now, it cannot be retrieved again.
console.log(data.secret);
```

### Python

```python
import requests

response = requests.post(
    "https://my.neuronai.uz/api/v1/api-keys",
    headers={"Authorization": "Bearer <your-api-key>"},
    json={
        "name": "CI",
        "access": "restricted",
        "permissions": {"tts": "write"},
        "expires_at": "2030-01-01T00:00:00Z",
    },
)

response.raise_for_status()
data = response.json()["data"]

# Shown only this once — store it now, it cannot be retrieved again.
print(data["secret"])
```

### PHP

```php
$ch = curl_init('https://my.neuronai.uz/api/v1/api-keys');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer <your-api-key>',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'name' => 'CI',
        'access' => 'restricted',
        'permissions' => ['tts' => 'write'],
        'expires_at' => '2030-01-01T00:00:00Z',
    ]),
]);

$response = json_decode(curl_exec($ch), true);
curl_close($ch);

// Shown only this once — store it now, it cannot be retrieved again.
echo $response['data']['secret'];
```

---

Source: https://docs.neuronai.uz/ru/api/reference/api-keys/create-api-key
