> ## Documentation Index
> Fetch the complete documentation index at: https://tyk.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Set Up an Automated Client With a Workspace in Tyk AI Studio

> Give an automated client, such as a CI pipeline or a script, a service account, its own Workspace, and a global push role, and push only when no changes from other Workspaces are pending.

## Availability

| Edition | Deployment Type |
| :- | :- |
| [Enterprise](https://tyk.io/docs/ai-management/ai-studio/overview.md#enterprise-edition) | Self-Managed |

Workspaces are available from Tyk AI Studio 2.3.0.

An automated client, such as a CI pipeline or a script, manages AI Studio objects through the management API. It also pushes configuration to the Edge Gateways. Give the client its own Workspace so that it manages only its own objects. For how Workspaces work, refer to [Workspaces](https://tyk.io/docs/ai-management/ai-studio/workspaces.md).

## Set Up the Client

1. Create a service account. Go to **Access > Users**, click **Add user**, and set **Account type** to **Service account**. Copy the API key. AI Studio shows the key one time only. Refer to [Service Accounts](https://tyk.io/docs/ai-management/ai-studio/users.md#service-accounts).
2. Create a Workspace for the client. Go to **Access > Workspaces** and click **Add Workspace**.
3. Give the service account a role in that Workspace. On the **Access** tab of the Workspace, add the service account with the Editor role. A custom role with fewer permissions also works.
4. Give the service account a global role for pushes. Create a [custom role](https://tyk.io/docs/ai-management/ai-studio/rbac.md#manage-roles) with only the `edges:read` and `edges:execute` permissions. Assign it to the service account as a global role, in the **Roles** field of the user.
5. Create a namespace for each Team that the client pushes to.

## Push Only the Changes of the Workspace

The client lists and changes only the objects of its Workspace. Before a push, it reads the pending changes of the namespace for its Workspace. The `stained` and `outside_total` fields show if changes from other Workspaces are pending.

To push only when no such changes are pending, the client sends `only_workspace_id` in the push request. For the request format, refer to the [AI Studio API](https://tyk.io/docs/ai-management/ai-studio/ai-studio-swagger.md).

* If changes from other Workspaces are pending, AI Studio returns `409` with the code `push_stained`, and does not push.
* Objects with no Workspace, such as plugins and model prices, count as changes from other Workspaces.

If changes from other Workspaces are pending when the client starts, its first push with `only_workspace_id` fails. To start clean, push the namespace one time from the admin console before you start the client.

## Example API Calls

This example shows the setup and the calls of the client. `$ADMIN_KEY` is the API key of an administrator, and `$SA_KEY` is the API key of the service account. The IDs are examples.

```bash expandable theme={null}
# Administrator: create the Workspace of the client (the response contains its ID, here 4)
curl -X POST "$STUDIO/api/v1/workspaces" -H "Authorization: Bearer $ADMIN_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"name": "ci-team-a", "description": "Objects that the CI pipeline manages for Team A"}'

# Administrator: create the service account (the response contains the API key, one time only)
curl -X POST "$STUDIO/api/v1/users" -H "Authorization: Bearer $ADMIN_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"data": {"type": "users", "attributes": {"account_type": "service", "name": "ci-team-a"}}}'

# Administrator: create a push role and assign it as a global role (no scope)
curl -X POST "$STUDIO/api/v1/rbac/roles" -H "Authorization: Bearer $ADMIN_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"name": "Automation push", "description": "Read the sync state and push configuration", "permissions": ["edges:read", "edges:execute"]}'
curl -X POST "$STUDIO/api/v1/rbac/bindings" -H "Authorization: Bearer $ADMIN_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"subject_type": "user", "subject_id": 44, "role_id": 9}'

# Administrator: assign Editor within the Workspace (get the ID of Editor from GET /api/v1/rbac/roles)
curl -X POST "$STUDIO/api/v1/rbac/bindings" -H "Authorization: Bearer $ADMIN_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"subject_type": "user", "subject_id": 44, "role_id": 3, "scope_type": "workspace", "scope_id": "4"}'

# Client: check if a push would include changes from other Workspaces ("stained": true or false)
curl "$STUDIO/api/v1/sync/pending-changes?namespace=default&workspace_id=4" \
  -H "Authorization: Bearer $SA_KEY"

# Client: push only if no changes from other Workspaces are pending (202, or 409 push_stained)
curl -X POST "$STUDIO/api/v1/namespaces/default/reload" -H "Authorization: Bearer $SA_KEY" \
  -H 'Content-Type: application/json' -d '{"only_workspace_id": 4}'
```
