# Assign credits to a user

> Moves credits from the partner's balance to this user's own balance.

Source: https://docs.spicrawl.com/api-reference/partner/partner-workspace-assign-credits

## POST /v1/partner/workspaces/{external_user_id}/credits

Operation ID: `partnerWorkspaceAssignCredits`. API key scope: `partner`.

Moves credits from the partner's balance to this user's own balance. The user spends them after their free
credits and before the partner's balance. They do not reset each month. The funded balance reaches requests
at once on the API process that took the assignment, and within about 5 seconds on the others.

Idempotent per `idempotency_key`: a repeat with the same key, the same `credits` and the same user returns
`"applied": false` and changes nothing. A key reused with a different amount or user is
`409 ERR::REQUEST::CONFLICT` and changes nothing. The partner can assign at most its balance (funded less
spent, never the credit line). More than that is `402 ERR::LIMIT::QUOTA_EXCEEDED`. The partner's record of the
assignment is a transfer out, and the workspace's is a transfer in.

### Example

```bash
curl -X POST "https://api.spicrawl.com/v1/partner/workspaces/{external_user_id}/credits" \
  -H "Authorization: Bearer $SPICRAWL_API_KEY"
```

### Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `external_user_id` | path | string | yes | The partner's own id for the end user. |

### Request body

`application/json`, required.

| Field | Type | Required | Description |
|---|---|---|---|
| `credits` | integer | yes | Whole credits to move from the partner's balance to the user's own balance. At most the partner's balance (funded less spent); the credit line cannot be assigned. |
| `idempotency_key` | string | yes | 8 to 100 characters, no spaces. Use a new key for each assignment; a repeat with the same key changes nothing. |

### Responses

#### 200

The assignment, applied or already applied.

`application/json`.

| Field | Type | Required | Description |
|---|---|---|---|
| `external_user_id` | string | yes |  |
| `credits` | integer (int64) | yes |  |
| `applied` | boolean | yes | False when the idempotency key was used before. Nothing changed. |
| `user_credits` | integer (int64) | yes | The user's own assigned balance left, in whole credits. |
| `balance_available` | integer (int64) | yes | The partner's assignable balance (funded less spent, floored at zero, credit line excluded) after this assignment, in whole credits. The field name is kept for compatibility. |

#### 400

The request is invalid. Not retryable; fix the request using `code` and `diagnostics.hint`.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 401

Missing, invalid, revoked or expired key. Not retryable with the same key.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 402

`ERR::LIMIT::QUOTA_EXCEEDED`: the organization is out of credits. Not retryable until credits are added. For a partner workspace the message says its free credits are used, with the date they return; once the partner's balance or the workspace's own balance has been funded and is exhausted, it says the provider's balance is exhausted and the partner must add balance.

Headers: `X-Request-Id`, `X-Credits-Remaining`.

`application/problem+json` (`Problem` schema).

#### 403

The key lacks the scope this route requires (`ERR::AUTH::INSUFFICIENT_SCOPE`), or the action is not permitted.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 404

No such resource for this key's organization.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 409

Conflicts with the resource's current state, for example a session in use (`ERR::SESSION::BUSY`, retryable).

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 500

Internal error. Retryable when `retryable` is true.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

Full OpenAPI spec: https://docs.spicrawl.com/openapi.yaml
