# Create or get a user

Register your application’s user ID. Repeating this request returns the same user without changing their details.

`POST https://api.smallworld.sh/api/v1/users`

## Parameters

| Name | Location | Type | Required | Description |
| --- | --- | --- | --- | --- |
| userId | body | string | Yes | Your user's unique identifier in this workspace. At most 512 characters, without surrounding whitespace or null bytes. The older name id is still accepted; sending both with different values is invalid. |
| email | body | string \| null | No | Email for a new user: a valid address of at most 254 characters after trimming. Omit, null, or an empty string stores null. Existing users are unchanged; use PATCH to update their email. |
| groupId | body | string \| null | No | Moves a new or an existing user into this group. Omit to leave their memberships unchanged, or send null to remove them from every group. The group must already exist. Not together with groupIds. |
| groupIds | body | string[] | No | Replaces all of the user's memberships with this list, for a new or an existing user. An empty list removes them from every group, and duplicates count once. Every group must already exist. Not together with groupId. |

## Request examples

### curl

```curl
curl --fail-with-body -X POST 'https://api.smallworld.sh/api/v1/users' \
  -H "Authorization: Bearer $SMALLWORLD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "userId": "customer_123",
  "email": "customer@example.com"
}'
```

### typescript

```typescript
const response = await fetch("https://api.smallworld.sh/api/v1/users", {
  method: "POST",
  headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
  body: JSON.stringify({
  "userId": "customer_123",
  "email": "customer@example.com"
}),
});
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json={
  "userId": "customer_123",
  "email": "customer@example.com"
},
    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.

### 201 · Created

The user was created. You can now discover their tools or connect an integration.

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

### 200 · Existing

This user already exists. Their original email is returned unchanged; groupId or groupIds in the request still changes their groups.

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

### 400

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

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

### 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, whose group can't change, 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, whose group can't change, 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"
}
```

### 409

user_in_multiple_groups: groupId was sent for a user who belongs to several groups. Replace their memberships with groupIds, or add and remove single memberships.

#### user_in_multiple_groups

groupId was sent for a user who belongs to several groups. Replace their memberships with groupIds, or add and remove single memberships.

```json
{
  "statusCode": 409,
  "code": "user_in_multiple_groups",
  "message": "This user belongs to several groups, so they can't be moved. Add or remove single memberships, or replace them all with groupIds."
}
```

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

- Public user IDs are identifiers from your application, not Smallworld's internal database IDs. Derive them from your authenticated user on your backend.

- Returns 201 when created, or 200 when this ID already exists. Repeated requests keep the existing email, even if you supply a different email.

- User objects have groupIds and no longer have groupId. This is a breaking change: read user.groupIds wherever you read user.groupId, including the user in execution responses and the members of a group.

- A user can belong to any number of groups. groupId is a move, also for existing users: a user with no group joins, and a user in exactly one other group leaves it and joins this one, with their open sessions that acted for the old group now acting for the new one. For a user in several groups it returns user_in_multiple_groups, also when it names a group they are already in. groupId: null removes the user from every group.

- groupIds replaces the whole list in one step. When it replaces exactly one group with exactly one other, it is a move and the user's sessions follow. Otherwise sessions that acted for a removed group are closed. groupId: null closes them in the same way. Sending groupId together with groupIds is invalid.

- An unknown group in groupId or groupIds returns group_not_found, and nothing is created or changed, not even the user. To add one group and keep the others, use PUT /users/:userId/groups/:groupId.

- Create a user before loading their /worlds/:worldId/tools catalog. Execution and connect-session creation can also create users automatically.
