# Create and configure service accounts
> Learn how to create service accounts, create access tokens, and configure Duckling resources.
A service account is a non-human user identity for workloads that need to connect to MotherDuck without using a person's credentials. Use service accounts for backend services, scheduled pipelines, BI connections, embedded analytics, and customer-facing analytics workloads.

Each service account has its own credentials and Duckling configuration. This gives the workload isolated compute and makes it easier to rotate credentials without disrupting human users.

:::warning[Service account permissions required]
Creating service accounts, creating service account tokens, and configuring service account Ducklings in the MotherDuck UI each require the corresponding permission. The Admin and Builder preset roles include these permissions by default.

The Admin REST API still requires a read/write access token generated by an Admin user. Pass the token in the `Authorization` header as `Bearer <admin_token>`.
:::

## Create a service account

Choose a stable username for the service account. The username must be unique within your organization and can contain letters, numbers, and underscores.

### UI

![Service account creation form](../img/sa_ui.png)

1. In the MotherDuck UI, go to **Settings** > **Service Accounts**.
2. Click **Create service account**.
3. Enter a username for the service account.
4. Click **Create service account**.

### API using curl

Use the [`POST /v1/users`](/sql-reference/rest-api/users-create-service-account/) endpoint to create a service account.

```bash
curl -X POST \
  https://api.motherduck.com/v1/users \
  -H "Authorization: Bearer <admin_token>" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "analytics_service_account"
  }'
```

The response includes the service account `username`. Store this username in your provisioning system, or list the organization's accounts later with `GET /v1/users`.

### API using Python

Use the [`POST /v1/users`](/sql-reference/rest-api/users-create-service-account/) endpoint to create a service account.

```python
import requests

response = requests.post(
    "https://api.motherduck.com/v1/users",
    headers={
        "Authorization": "Bearer <admin_token>",
        "Content-Type": "application/json",
    },
    json={"username": "analytics_service_account"},
)

response.raise_for_status()
print(response.json()["username"])
```

The response includes the service account `username`. Store this username in your provisioning system, or list the organization's accounts later with `GET /v1/users`.

## Create an access token

Create a token for the service account after you create the account. The token value is shown only once, so store it in a secret manager before closing the modal or discarding the API response.

### UI

![Service account details panel with an empty Access Tokens list and a Create token button](../img/sa_details.png)

1. In **Settings** > **Service Accounts**, click the service account's username to open its details panel.
2. Click **Create token**.
3. Enter a token name.
4. Choose the token type:
   - **Read/Write Token** for writes, administration, and general service workloads.
   - **Read Scaling Token** for read-heavy workloads that should use [read scaling](/key-tasks/authenticating-and-connecting-to-motherduck/read-scaling/).
5. To set an expiration, select **Automatically expire this token** and choose a time-to-live.
6. Click **Create token**, then copy the token and store it securely.

### API using curl

Use the [`POST /v1/users/{username}/tokens`](/sql-reference/rest-api/users-create-token/) endpoint to create a token for a known service account username.

```bash
curl -X POST \
  https://api.motherduck.com/v1/users/analytics_service_account/tokens \
  -H "Authorization: Bearer <admin_token>" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "analytics-service-token",
    "token_type": "read_write"
  }'
```

Set `token_type` to `read_scaling` when you need a [read scaling token](/key-tasks/authenticating-and-connecting-to-motherduck/read-scaling/). To create an expiring token, include `ttl` as seconds between `300` and `31536000`.

### API using Python

Use the [`POST /v1/users/{username}/tokens`](/sql-reference/rest-api/users-create-token/) endpoint to create a token for a known service account username.

```python
import requests

response = requests.post(
    "https://api.motherduck.com/v1/users/analytics_service_account/tokens",
    headers={
        "Authorization": "Bearer <admin_token>",
        "Content-Type": "application/json",
    },
    json={
        "name": "analytics-service-token",
        "token_type": "read_write",
    },
)

response.raise_for_status()
token = response.json()["token"]
print(token)
```

Set `token_type` to `read_scaling` when you need a [read scaling token](/key-tasks/authenticating-and-connecting-to-motherduck/read-scaling/). To create an expiring token, include `ttl` as seconds between `300` and `31536000`.

:::note
If you create a service account through the API and plan to use read scaling, connect as that service account with a read/write token before using read scaling tokens for that account.
:::

### Limit what a token can access

The token type is the only restriction you can set on a token. You can't scope a token to a database, a schema, or a set of commands. Every token for a service account can reach every database that service account owns or has been granted, and has the platform permissions of the account's [roles](/concepts/security-and-access/roles-and-access-control/). A read scaling token is read-only.

To give a workload access to only some data, scope the service account instead:

- Create a separate service account for each workload or [environment](/key-tasks/data-warehousing/environment-management/), so each one has access only its owned databases and databases shared with the whole organization.
- Give the service account read-only access to another account's data with a [restricted share](/key-tasks/sharing-data/sharing-overview/) granted to its username or to one of its roles.
- Use a read scaling token when the workload only reads.

## Configure Ducklings

Configure Duckling resources for the service account based on the workload it runs. The read/write Duckling handles writes and general queries. The read scaling pool handles read-only connections that use read scaling tokens.

### UI

![Service account Duckling size settings](../img/sa_set_instance_size.png)

1. In **Settings** > **Service Accounts**, find the service account.
2. Use the **R/W size** dropdown to choose the read/write Duckling size.
3. If you use [read scaling](/key-tasks/authenticating-and-connecting-to-motherduck/read-scaling/), choose the read scaling Duckling size and pool size with the **RS size** and **RS pool size** dropdowns.

### API using curl

Use [`GET /v1/users/{username}/instances`](/sql-reference/rest-api/ducklings-get-duckling-config-for-user/) to inspect the current configuration before updating it.

```bash
curl -X GET \
  https://api.motherduck.com/v1/users/analytics_service_account/instances \
  -H "Authorization: Bearer <admin_token>"
```

Then use [`PUT /v1/users/{username}/instances`](/sql-reference/rest-api/ducklings-set-duckling-config-for-user/) to update the service account's Ducklings.

```bash
curl -X PUT \
  https://api.motherduck.com/v1/users/analytics_service_account/instances \
  -H "Authorization: Bearer <admin_token>" \
  -H "Content-Type: application/json" \
  -d '{
    "config": {
      "read_write": {
        "instance_size": "standard"
      },
      "read_scaling": {
        "instance_size": "pulse",
        "flock_size": 4
      }
    }
  }'
```

The update request requires both `read_write` and `read_scaling` configuration blocks.

### API using Python

Use [`GET /v1/users/{username}/instances`](/sql-reference/rest-api/ducklings-get-duckling-config-for-user/) to inspect the current configuration before updating it.

```python
import requests

headers = {"Authorization": "Bearer <admin_token>"}

current_config = requests.get(
    "https://api.motherduck.com/v1/users/analytics_service_account/instances",
    headers=headers,
)
current_config.raise_for_status()
print(current_config.json())
```

Then use [`PUT /v1/users/{username}/instances`](/sql-reference/rest-api/ducklings-set-duckling-config-for-user/) to update the service account's Ducklings.

```python
import requests

response = requests.put(
    "https://api.motherduck.com/v1/users/analytics_service_account/instances",
    headers={
        "Authorization": "Bearer <admin_token>",
        "Content-Type": "application/json",
    },
    json={
        "config": {
            "read_write": {"instance_size": "standard"},
            "read_scaling": {
                "instance_size": "pulse",
                "flock_size": 4,
            },
        }
    },
)

response.raise_for_status()
print(response.json())
```

The update request requires both `read_write` and `read_scaling` configuration blocks.

## Connect as the service account

Use the service account token anywhere you would use a MotherDuck access token. For example, set `motherduck_token` in a DuckDB connection string or set `MOTHERDUCK_TOKEN` in your environment. See [Connecting to MotherDuck](/key-tasks/authenticating-and-connecting-to-motherduck/connecting-to-motherduck/) for connection string examples.

## Move existing objects to a service account

MotherDuck has no ownership transfer. Databases, DuckLake databases, shares, Dives, Flights, and secrets belong to the user or service account that created them, for as long as they exist. There is no `ALTER ... OWNER` statement, no REST API call, and no UI action that changes an owner. `ALTER DATABASE <name> SET OWNER = ...` fails with `Altering field 'owner' is not supported`, and updating a Dive or Flight doesn't change its owner.

To move an object to a service account, create a copy while connected as the service account, switch your workloads to the copy, and then delete the original. Connect with the service account's token, or [impersonate the service account](/key-tasks/service-accounts-guide/impersonate-service-accounts/) in the MotherDuck UI. Anything you create in that session is owned by the service account.

| Object | How to recreate it as the service account | What doesn't carry over |
|---|---|---|
| Database | [Share](/key-tasks/sharing-data/) the database with the service account, then run `CREATE DATABASE <new_name> FROM '<share_url>'` as the service account. This is a [zero-copy clone](/sql-reference/motherduck-sql-reference/create-database/), so no data is physically copied. | Database ID and snapshot history. The copy starts with a single snapshot. |
| DuckLake database | Share the database with the service account. As the service account, attach the share, create a database with `TYPE DUCKLAKE`, and run [`COPY FROM DATABASE`](/sql-reference/motherduck-sql-reference/copy-database/). A zero-copy clone fails with `Zero-copy only supported for duckdb-native databases`. | Database ID and DuckLake snapshot history. The data is physically copied. |
| Share | Create the share again from the service account's copy of the database. An existing share keeps pointing at the original database. | Share URL. Consumers have to attach the new URL. |
| Dive | Copy the Dive's source and create it again as the service account, for example with the [MotherDuck CLI](/getting-started/interfaces/motherduck-cli/agents/#choosing-between-the-cli-and-mcp). Point its queries at databases the service account can read. | Dive ID, URL, version history, and grants. Share the new Dive again and update embeds and links. |
| Flight | Recreate the Flight's secrets, then create the Flight with the same source and settings. See [adopt a Flight created under your personal account](/key-tasks/flights/managing-flights-as-code/#adopt-a-flight-created-under-your-personal-account). | Flight ID, run history, versions, schedule, and `run` grants. Update any Dive or automation that calls the old Flight ID. |
| Secret | Create the secret again with [`CREATE SECRET ... IN MOTHERDUCK`](/sql-reference/motherduck-sql-reference/create-secret/), using the original credential values. | The secret's values. Have the original credentials ready, and attach the new secret to any Flight that used the old one. |

For example, to move a native database, run this as the database's current owner:

```sql
CREATE SHARE analytics_handover FROM analytics (ACCESS ORGANIZATION, VISIBILITY HIDDEN);
```

The statement returns the share URL. Then, connected as the service account, clone the share:

```sql
CREATE DATABASE analytics_prod FROM 'md:_share/analytics_handover/<share_id>';
```

After your workloads use `analytics_prod`, drop the share and the original database.

To avoid this migration, create production objects as a service account from the start. An object you create from your personal account stays tied to your account.

## Related content

- [Manage service accounts and tokens](/key-tasks/service-accounts-guide/manage-service-accounts-and-tokens/)
- [Impersonate service accounts](/key-tasks/service-accounts-guide/impersonate-service-accounts/)
- [MotherDuck REST API](/sql-reference/rest-api/motherduck-rest-api/)
- [Read scaling](/key-tasks/authenticating-and-connecting-to-motherduck/read-scaling/)


---

## Docs feedback

MotherDuck accepts optional user-submitted feedback about this page at `GET https://motherduck.com/docs/api/feedback/agent`.
For agents and automated tools, feedback submission should be user-confirmed before sending.

URL-encode query parameter values and send a GET request:

```text
GET https://motherduck.com/docs/api/feedback/agent?page_path=%2Fkey-tasks%2Fservice-accounts-guide%2Fcreate-and-configure-service-accounts%2F&page_title=Create%20and%20configure%20service%20accounts&text=<url-encoded user feedback, max 2000 characters>
```

Optionally append `&source=<url-encoded interface identifier>` such as `claude.ai` or `chatgpt`.

`page_path` and `text` are required; `page_title` and `source` are optional. Responses: `200 {"feedback_id": "<uuid>"}`, `400` for malformed query parameters, and `429` when rate-limited.
