# Save API-key credentials

Validate and save API-key credentials your backend collected for a represented user.

`POST https://api.smallworld.sh/api/v1/users/:userId/connections/:integration`

## Parameters

| Name | Location | Type | Required | Description |
| --- | --- | --- | --- | --- |
| userId | path | string | Yes | Your application's user ID, URL-encoded. At most 512 characters, with no surrounding whitespace or null bytes. Encode the IDs `.` and `..` as `%2E` and `%2E%2E`: curl sends these as written, but JavaScript's `fetch` removes them even when encoded. |
| integration | path | string | Yes | An integration key from GET /integrations, such as github or notion. |
| groupId | query | string | No | Your application's group ID: which of the user's groups the result is for. Omit it for a user with no group or one group. Required when the user belongs to several groups. URL-encode it. |
| credentials | body | object | Yes | String values keyed by the integration's credentialFields names. Each value is at most 4096 characters; provider-specific rules also apply. |

## Request examples

### curl

```curl
curl --fail-with-body -X POST 'https://api.smallworld.sh/api/v1/users/customer_123/connections/github' \
  -H "Authorization: Bearer $SMALLWORLD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "credentials": {
    "token": "YOUR_USER_GITHUB_TOKEN"
  }
}'
```

### typescript

```typescript
const response = await fetch("https://api.smallworld.sh/api/v1/users/customer_123/connections/github", {
  method: "POST",
  headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
  body: JSON.stringify({
  "credentials": {
    "token": "YOUR_USER_GITHUB_TOKEN"
  }
}),
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
const result = await response.json();
```

### python

```python
response = requests.post(
    "https://api.smallworld.sh/api/v1/users/customer_123/connections/github",
    headers={"Authorization": f"Bearer {api_key}"},
    json={
  "credentials": {
    "token": "YOUR_USER_GITHUB_TOKEN"
  }
},
    timeout=30,
)
response.raise_for_status()
result = response.json()
```

## Responses

Returns integration, your userId, groupId, source, overridesGroup, authScheme, and status (connected, needs_reauth, or not_connected). The connection is the one the user's calls use in a session for the selected group: their own (source personal, groupId null) or, when they have none, that group's (source group, groupId set). overridesGroup is true when the user's own connection hides that group's connection to the same integration. source is null when not connected. accountId, accountName, workspaceId, connectedAt, updatedAt, and error are nullable. Credentials are never returned.

### 200 · Connected

The user's own account. The provider accepted the credentials; secret credential values are never returned.

```json
{
  "integration": "github",
  "userId": "customer_123",
  "groupId": null,
  "source": "personal",
  "overridesGroup": false,
  "authScheme": "api_key",
  "status": "connected",
  "accountId": "123456",
  "accountName": "octocat",
  "workspaceId": null,
  "connectedAt": "2026-10-10T12:00:01.000Z",
  "updatedAt": "2026-10-10T12:00:01.000Z",
  "error": null
}
```

### 400

invalid_request: A parameter or the body is invalid; the message names the problem. Unknown body fields are rejected. group_required: The user belongs to several groups and the request has no groupId. integration_invalid_provider: The integration key does not identify an available integration. integration_connection_not_supported: Built-in integrations have authScheme none and do not need a connection. integration_setup_required: An added MCP server needs its setup verified before anyone can connect. The message says what is missing. integration_credentials_not_supported: This integration uses OAuth; create a connect session instead. integration_credentials_invalid: Credential fields are invalid or the provider rejected them.

#### invalid_request

A parameter or the body is invalid; the message names the problem. Unknown body fields are rejected.

```json
{
  "statusCode": 400,
  "code": "invalid_request",
  "message": "Unknown field: color"
}
```

#### group_required

The user belongs to several groups and the request has no groupId.

```json
{
  "statusCode": 400,
  "code": "group_required",
  "message": "This user belongs to several groups. Send groupId to choose one."
}
```

#### integration_invalid_provider

The integration key does not identify an available integration.

```json
{
  "statusCode": 400,
  "code": "integration_invalid_provider",
  "message": "Unsupported integration provider 'acme'"
}
```

#### integration_connection_not_supported

Built-in integrations have authScheme none and do not need a connection.

```json
{
  "statusCode": 400,
  "code": "integration_connection_not_supported",
  "message": "Web Access is built in and available without a connection; there is nothing to connect"
}
```

#### integration_setup_required

An added MCP server needs its setup verified before anyone can connect. The message says what is missing.

```json
{
  "statusCode": 400,
  "code": "integration_setup_required",
  "message": "This server requires your own OAuth app. Enter its client ID and, if required by the provider, its client secret."
}
```

#### integration_credentials_not_supported

This integration uses OAuth; create a connect session instead.

```json
{
  "statusCode": 400,
  "code": "integration_credentials_not_supported",
  "message": "GitHub connects through OAuth. Create a connect session instead."
}
```

#### integration_credentials_invalid

Credential fields are invalid or the provider rejected them.

```json
{
  "statusCode": 400,
  "code": "integration_credentials_invalid",
  "message": "GitHub did not accept these credentials"
}
```

### 401

authentication_required: The Authorization header is missing or empty. invalid_api_key: The API key is invalid or revoked, or the header doesn't use the Bearer scheme.

#### authentication_required

The Authorization header is missing or empty.

```json
{
  "statusCode": 401,
  "code": "authentication_required",
  "message": "Authentication is required"
}
```

#### invalid_api_key

The API key is invalid or revoked, or the header doesn't use the Bearer scheme.

```json
{
  "statusCode": 401,
  "code": "invalid_api_key",
  "message": "The API key is invalid or revoked"
}
```

### 403

ip_not_allowed: The request came from an address outside the workspace's IP allowlist.

#### ip_not_allowed

The request came from an address outside the workspace's IP allowlist.

```json
{
  "statusCode": 403,
  "code": "ip_not_allowed",
  "message": "The source IP address is not allowed by this account's security settings"
}
```

### 404

group_not_found: groupId names a group that does not exist in your workspace.

#### group_not_found

groupId names a group that does not exist in your workspace.

```json
{
  "statusCode": 404,
  "code": "group_not_found",
  "message": "Group not found"
}
```

### 409

user_not_in_group: groupId names a group the user does not belong to. integration_disabled: Enable the integration in your workspace before connecting it. integration_setup_required: Complete the integration setup before connecting it.

#### user_not_in_group

groupId names a group the user does not belong to.

```json
{
  "statusCode": 409,
  "code": "user_not_in_group",
  "message": "The user does not belong to this group"
}
```

#### integration_disabled

Enable the integration in your workspace before connecting it.

```json
{
  "statusCode": 409,
  "code": "integration_disabled",
  "message": "GitHub is not enabled for this workspace. Add it under Integrations first."
}
```

#### integration_setup_required

Complete the integration setup before connecting it.

```json
{
  "statusCode": 409,
  "code": "integration_setup_required",
  "message": "Complete the integration setup before users can connect it"
}
```

### 413

request_too_large: The JSON body is larger than 12 MiB. File uploads count toward it, base64 or escaping included.

#### request_too_large

The JSON body is larger than 12 MiB. File uploads count toward it, base64 or escaping included.

```json
{
  "statusCode": 413,
  "code": "request_too_large",
  "message": "The request body is larger than 12 MiB."
}
```

### 415

unsupported_media_type: The request has a body that isn't JSON. Send JSON with Content-Type: application/json, or no body at all.

#### unsupported_media_type

The request has a body that isn't JSON. Send JSON with Content-Type: application/json, or no body at all.

```json
{
  "statusCode": 415,
  "code": "unsupported_media_type",
  "message": "Send the request body as JSON with Content-Type: application/json."
}
```

### 500

internal_error: An unexpected server error.

#### internal_error

An unexpected server error.

```json
{
  "statusCode": 500,
  "code": "internal_error",
  "message": "Internal server error"
}
```

### 502

integration_provider_unavailable: The provider could not be reached to verify credentials.

#### integration_provider_unavailable

The provider could not be reached to verify credentials.

```json
{
  "statusCode": 502,
  "code": "integration_provider_unavailable",
  "message": "GitHub could not be reached to check these credentials"
}
```

## Notes

- Fetch /integrations to discover the actual credential fields. This endpoint supports api_key, not oauth2 or none.

- Smallworld checks credentials with the provider before storing them encrypted. An unknown user is created after successful verification; reconnecting replaces the existing credentials.

- The credentials are the user's own and apply in all of their sessions. When the selected group has a connection to the same integration, the user's account replaces it for their calls, and the response shows overridesGroup true. To save credentials for a group, use POST /groups/:groupId/connections/:integration.

- groupId follows the rules of a new session in POST /worlds/:worldId/execute. Without it, a user with no groups is read without a group and a user with one group is read for that group. A user in several groups needs groupId. It chooses the group the returned connection is resolved for. When it fails, no credentials are saved.

- The hosted connect-session form is available when you want users to enter their keys without your application handling the credential.
