# Provision a workspace, or fetch it and its default key

> Creates the workspace for this end user if it does not exist, makes sure it has its default API key, and returns that key's plaintext.

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

## PUT /v1/partner/workspaces/{external_user_id}

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

Creates the workspace for this end user if it does not exist, makes sure it has its default
API key, and returns that key's plaintext. Calling it again is how the key is read again.

`201` when this call created the workspace, `200` when it already existed. `created` says which.
The key is normally the same on every call, but a new one is issued when the stored key has
expired or can no longer be read, so store whatever the latest call returned. The response is
sent with `Cache-Control: no-store`.

A partner may create at most a fixed number of workspaces per UTC day. Past that, a new user is
`429 ERR::LIMIT::PARTNER_WORKSPACE_LIMIT`. An existing user is never limited.

A workspace that has been soft-deleted is `409 ERR::REQUEST::CONFLICT` and returns no key: Spicrawl
support must restore it first. A closed workspace is `409 ERR::REQUEST::CONFLICT` too, with no key;
closure is terminal.

Rate limit: this route allows 2 requests per second per partner, with bursts of 20, on top of the
20-per-second limit every partner route shares.

The optional body names the tier a new workspace starts on, by id or name. Without it the workspace
gets the partner's default tier, or no tier when there is none. An existing workspace keeps its tier,
but an unknown tier is `404` either way. Move a workspace with `PUT .../tier`.

The optional `display_name` is a label for your own reference. It is optional; use any label you like,
and avoid personal data you do not need to share. It is applied only when this call creates the workspace:
an existing workspace keeps its name whatever body you send. Change it with `PATCH`.

### Example

```bash
curl -X PUT "https://api.spicrawl.com/v1/partner/workspaces/{external_user_id}" \
  -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`.

| Field | Type | Required | Description |
|---|---|---|---|
| `tier` | string | no | The tier's id or name. |
| `display_name` | string | no | Applied only when this call creates the workspace. Spaces at the ends are trimmed; a blank name is no name. Control characters are refused. |

### Responses

#### 200

The workspace already existed.

`application/json`.

| Field | Type | Required | Description |
|---|---|---|---|
| `workspace_id` | string | yes | The workspace's organization id. |
| `external_user_id` | string | yes |  |
| `display_name` | string \| null | no | The label the partner gave the workspace, or `null`. Set on `PUT` when that call creates the workspace, or with `PATCH`. Optional. The workspace's own key reports it as `organization.name` on `GET /v1/key`, so the end user can read it. |
| `status` | string: `active`, `suspended`, `closed`, `deleted` | yes | `deleted` when the workspace has been soft-deleted by Spicrawl support. A deleted workspace keeps its `external_user_id` but cannot be served; `PUT` on it is `409` and returns no key. |
| `created_at` | string (date-time) | yes |  |
| `free_credits` | integer | yes | The workspace's monthly allowance, in whole credits, as set for the partner. Always 0 while the partner is on a paid plan. |
| `wallet_cap` | integer | no | The most this workspace may draw from the partner's balance in one allowance month, in whole credits. Its own setting when Spicrawl set one, else the partner's default. `null` when it has no cap. Free credits and the workspace's own balance are not counted against it. A request that would need more than the cap left is refused with `402 ERR::LIMIT::QUOTA_EXCEEDED` ("...reached its spending limit for this month..."). A cap change reaches requests within about 5 seconds. |
| `tier` | object | no | The tier the workspace is on, or `null` when it has none. |
| `requests_per_minute` | integer | no | The tier's requests per minute for this user's scrape and browser requests, or `null` when there is no tier. |
| `concurrency` | integer | no | The tier's requests and browser sessions a user may run at once, or `null` when there is no tier. |
| `wallet_cap_source` | string: `tier`, `workspace`, `account_default`, `plan_default`, `none` | no | Where `wallet_cap` comes from. `tier`: the workspace's tier sets it. `workspace`: Spicrawl set it on this workspace while it is on no tier. `account_default`: the partner's default (a tier with no cap falls back to it). `plan_default`: the partner's paid plan sets it (none of the above is set). `none`: no cap, which only happens on the Free plan. |
| `window_resets_at` | string (date-time) | no | When the workspace's current allowance month ends (its monthly anniversary), RFC 3339 UTC. |
| `wallet_used` | integer \| null | no | Whole credits this user has drawn from the partner's balance in its current allowance month (floor). Compare with `wallet_cap`. Null on responses that do not compute it (`PUT`, suspend, reactivate) and when it cannot be read. |
| `wallet_used_source` | string \| null: `live`, `rollup`, `null` | no | `live` when read from the live counter, which is current. `rollup` when read from usage records, which trail by about 60 seconds. Null when `wallet_used` is null. |
| `free_used` | integer \| null | no | Whole free credits this user has used in its current allowance month (floor). Null on responses that do not compute it. |
| `own_credits` | integer \| null | no | Credits the partner gave this user that remain unspent, in whole credits (floor). The single `GET` reads it live; the list reads usage records, so it can trail by about 60 seconds. Null on responses that do not compute it, and when it cannot be read. |
| `created` | boolean | yes | True when this call created the workspace. |
| `api_key` | string | yes | The workspace's default API key, in full. Normally the same on every call; a new one is issued if the stored key has expired or can no longer be read. Store the latest one; it is the workspace's credential. |

#### 201

The workspace was created by this call.

`application/json`.

| Field | Type | Required | Description |
|---|---|---|---|
| `workspace_id` | string | yes | The workspace's organization id. |
| `external_user_id` | string | yes |  |
| `display_name` | string \| null | no | The label the partner gave the workspace, or `null`. Set on `PUT` when that call creates the workspace, or with `PATCH`. Optional. The workspace's own key reports it as `organization.name` on `GET /v1/key`, so the end user can read it. |
| `status` | string: `active`, `suspended`, `closed`, `deleted` | yes | `deleted` when the workspace has been soft-deleted by Spicrawl support. A deleted workspace keeps its `external_user_id` but cannot be served; `PUT` on it is `409` and returns no key. |
| `created_at` | string (date-time) | yes |  |
| `free_credits` | integer | yes | The workspace's monthly allowance, in whole credits, as set for the partner. Always 0 while the partner is on a paid plan. |
| `wallet_cap` | integer | no | The most this workspace may draw from the partner's balance in one allowance month, in whole credits. Its own setting when Spicrawl set one, else the partner's default. `null` when it has no cap. Free credits and the workspace's own balance are not counted against it. A request that would need more than the cap left is refused with `402 ERR::LIMIT::QUOTA_EXCEEDED` ("...reached its spending limit for this month..."). A cap change reaches requests within about 5 seconds. |
| `tier` | object | no | The tier the workspace is on, or `null` when it has none. |
| `requests_per_minute` | integer | no | The tier's requests per minute for this user's scrape and browser requests, or `null` when there is no tier. |
| `concurrency` | integer | no | The tier's requests and browser sessions a user may run at once, or `null` when there is no tier. |
| `wallet_cap_source` | string: `tier`, `workspace`, `account_default`, `plan_default`, `none` | no | Where `wallet_cap` comes from. `tier`: the workspace's tier sets it. `workspace`: Spicrawl set it on this workspace while it is on no tier. `account_default`: the partner's default (a tier with no cap falls back to it). `plan_default`: the partner's paid plan sets it (none of the above is set). `none`: no cap, which only happens on the Free plan. |
| `window_resets_at` | string (date-time) | no | When the workspace's current allowance month ends (its monthly anniversary), RFC 3339 UTC. |
| `wallet_used` | integer \| null | no | Whole credits this user has drawn from the partner's balance in its current allowance month (floor). Compare with `wallet_cap`. Null on responses that do not compute it (`PUT`, suspend, reactivate) and when it cannot be read. |
| `wallet_used_source` | string \| null: `live`, `rollup`, `null` | no | `live` when read from the live counter, which is current. `rollup` when read from usage records, which trail by about 60 seconds. Null when `wallet_used` is null. |
| `free_used` | integer \| null | no | Whole free credits this user has used in its current allowance month (floor). Null on responses that do not compute it. |
| `own_credits` | integer \| null | no | Credits the partner gave this user that remain unspent, in whole credits (floor). The single `GET` reads it live; the list reads usage records, so it can trail by about 60 seconds. Null on responses that do not compute it, and when it cannot be read. |
| `created` | boolean | yes | True when this call created the workspace. |
| `api_key` | string | yes | The workspace's default API key, in full. Normally the same on every call; a new one is issued if the stored key has expired or can no longer be read. Store the latest one; it is the workspace's credential. |

#### 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).

#### 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).

#### 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).

#### 429

Either the partner's daily allowance of new workspaces is used up (`ERR::LIMIT::PARTNER_WORKSPACE_LIMIT`,
`Retry-After` runs to the next 00:00 UTC), or the partner's rate limit for this route is exceeded
(`ERR::LIMIT::RATE_LIMITED`, `Retry-After` in seconds).

`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
