# Add a user to a group

Add a represented user to a group and keep their other groups. An unknown user is created.

`PUT https://api.smallworld.sh/api/v1/users/:userId/groups/:groupId`

## 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. |
| groupId | path | string | Yes | Your application's group ID, URL-encoded. At most 512 characters, with no surrounding whitespace or null bytes. |

## Request examples

### curl

```curl
curl --fail-with-body -X PUT 'https://api.smallworld.sh/api/v1/users/customer_123/groups/team_42' \
  -H "Authorization: Bearer $SMALLWORLD_API_KEY"
```

### typescript

```typescript
const response = await fetch("https://api.smallworld.sh/api/v1/users/customer_123/groups/team_42", {
  method: "PUT",
  headers: { Authorization: `Bearer ${apiKey}` },
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
const result = await response.json();
```

### python

```python
response = requests.put(
    "https://api.smallworld.sh/api/v1/users/customer_123/groups/team_42",
    headers={"Authorization": f"Bearer {api_key}"},
    timeout=30,
)
response.raise_for_status()
result = response.json()
```

## Responses

Returns id, nullable email, groupIds, and ISO 8601 createdAt and updatedAt timestamps. The ID is your application's identifier; groupIds lists your IDs for the user's current groups, ordered by ID, and is empty when they belong to none.

### 200

Returns id, nullable email, groupIds, and ISO 8601 createdAt and updatedAt timestamps. The ID is your application's identifier; groupIds lists your IDs for the user's current groups, ordered by ID, and is empty when they belong to none.

```json
{
  "id": "customer_123",
  "email": "customer@example.com",
  "groupIds": [
    "team_42",
    "team_77"
  ],
  "createdAt": "2026-10-10T12:00:00.000Z",
  "updatedAt": "2026-10-10T12:00:00.000Z"
}
```

### 400

invalid_request: A path parameter is invalid; the message names the problem.

#### invalid_request

A path parameter is invalid; the message names the problem.

```json
{
  "statusCode": 400,
  "code": "invalid_request",
  "message": "userId must be a non-empty string of at most 512 characters without surrounding whitespace or null bytes"
}
```

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

group_membership_protected: The user is the Playground user, who belongs only to the Playground Group, or the group is the Playground Group, which no other user can join. ip_not_allowed: The request came from an address outside the workspace's IP allowlist.

#### group_membership_protected

The user is the Playground user, who belongs only to the Playground Group, or the group is the Playground Group, which no other user can join.

```json
{
  "statusCode": 403,
  "code": "group_membership_protected",
  "message": "The Playground user always belongs to the Playground Group"
}
```

#### 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: The group does not exist in your workspace. Create groups from your backend with POST /groups.

#### group_not_found

The group does not exist in your workspace. Create groups from your backend with POST /groups.

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

### 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"
}
```

## Notes

- The user keeps their other groups. A user can belong to any number of groups.

- Adding a user to a group they are already in changes nothing, so a retry is safe.

- An unknown userId is created without an email. The group must already exist; for an unknown group, nothing is created.

- The user's open sessions are not affected: each keeps the group it was created for, and a groupless session stays groupless. Once the user belongs to several groups, a new session needs groupId in POST /worlds/:worldId/execute, and reads such as GET /users/:userId/connections need ?groupId=.

- The Playground user belongs only to the Playground Group, and no other user can join the Playground Group.
