
## Delete a provider key

`DELETE /v1/ai/credentials/:id`

Deletes a connected provider key. Related Custom provider models bound to this key are cascade-deleted. Usage history (`AiUsageLog`) is preserved — statistics records are not lost.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|----------|
| `id` (path) | string | yes | Key ID from [`GET /v1/ai/credentials`](./list.md) |

## Examples

### curl — personal key

```bash
curl -X DELETE https://vibecode.bitrix24.com/v1/ai/credentials/cred_abc123def456 \
  -H "X-Api-Key: YOUR_API_KEY"
```

### curl — OAuth application

```bash
curl -X DELETE https://vibecode.bitrix24.com/v1/ai/credentials/cred_abc123def456 \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN"
```

### JavaScript — personal key

```javascript
const id = 'cred_abc123def456'
const res = await fetch(`https://vibecode.bitrix24.com/v1/ai/credentials/${id}`, {
  method: 'DELETE',
  headers: { 'X-Api-Key': 'YOUR_API_KEY' },
})

const { success } = await res.json()
if (success) console.log('Key deleted')
```

### JavaScript — OAuth application

```javascript
const id = 'cred_abc123def456'
const res = await fetch(`https://vibecode.bitrix24.com/v1/ai/credentials/${id}`, {
  method: 'DELETE',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
  },
})

if ((await res.json()).success) console.log('OK')
```

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `success` | boolean | Always `true` on success |

## Response example

HTTP status `200 OK`:

```json
{
  "success": true
}
```

## Error response example

`404 not_found` — the key was not found or belongs to another user:

```json
{
  "success": false,
  "error": {
    "code": "not_found",
    "message": "Credential not found"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|----------|
| 404 | `not_found` | A key with this `id` was not found or does not belong to you |
| 403 | `scope_missing` | The API key lacks the `vibe:ai` scope |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |

Full list of common API errors — [Errors](/docs/errors).

## Known specifics

**Cascade deletion of models.** For a Custom provider whose key has models bound via [`POST /v1/ai/credentials/:id/models`](./models-add.md) or [`POST /v1/ai/credentials/:id/fetch-models`](./fetch-models.md), all bound models are deleted together with the key.

**Usage history is preserved.** `AiUsageLog` records are not deleted — statistics for this provider from past periods remain in [`GET /v1/ai/usage`](/docs/ai/consumption/usage). The `modelId` and `providerId` fields are stored as strings, with no `FK` relation to the keys table.

**USER keys only.** The endpoint works only with the current user's keys. PORTAL keys that an administrator connected for all users are not available here.

## See also

- [Key list](./list.md)
- [Update a key](./update.md)
- [Your own keys (BYOK)](/docs/ai/credentials)
