# Connect a worker

Connect a coding agent on your machine, such as Claude Code, as a worker that takes a project's runs and answers them. It asks Veryfront for work, so it needs no public address.

## Before you start

You need a project with an agent. See [Create an agent](/docs/agents/create-agent.md).

## Connect a worker

**Studio**

1. In your project, open **Agents**.
2. Click the **Connected** tab.
3. Click **Connect agent**.
4. Copy the **Project reference**.
5. In a terminal, register the worker with that reference as `$PROJECT` and an [API key](/docs/authentication/api-keys.md):

```bash
curl -X POST https://api.veryfront.com/agent-workers/projects/$PROJECT/workers \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"implementation_kind": "claude-code", "implementation_display_name": "Claude Code", "worker_key": "laptop", "display_name": "Laptop"}'
```

6. Send its first heartbeat, with `WORKER_ID` set to the answer's `worker.id` and `WORKER_TOKEN` to its `token`:

```bash
export WORKER_ID=<worker.id>
export WORKER_TOKEN=<token>
curl -X POST https://api.veryfront.com/agent-workers/workers/$WORKER_ID/heartbeat \
  -H "Authorization: Bearer $WORKER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

7. Click **Refresh** on **Connected**. You should now see Laptop with **Online**.

**Code**

1. Install the SDK:

```bash
npm install veryfront
```

2. Save the worker. It registers itself with your [API key](/docs/authentication/api-keys.md), then sends a heartbeat so it stays online:

```js title="worker.mjs"
import { createExternalAgentWorkerClient } from "veryfront/agent";

const client = createExternalAgentWorkerClient({
  apiUrl: process.env.VERYFRONT_API_URL ?? "https://api.veryfront.com",
  authToken: process.env.VERYFRONT_API_KEY,
});
const worker = await client.registerWorker({
  projectReference: process.env.PROJECT,
  implementationKind: "claude-code",
  implementationDisplayName: "Claude Code",
  workerKey: "laptop",
  displayName: "Laptop",
});
const beat = await client.heartbeatWorker(worker.id);
console.log(`${beat.display_name}: ${beat.status}`);
```

3. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
4. Run it. You should now see `Laptop: active`, and Laptop **Online** under **Agents** > **Connected** in Studio:

```bash
node worker.mjs
```

### Try it in a sandbox

```playground
step: Install the SDK | npm install veryfront
step: Save the worker | printf '%s\n' 'import { createExternalAgentWorkerClient } from "veryfront/agent";' '' 'const client = createExternalAgentWorkerClient({' '  apiUrl: process.env.VERYFRONT_API_URL ?? "https://api.veryfront.com",' '  authToken: process.env.VERYFRONT_API_KEY,' '});' 'const worker = await client.registerWorker({' '  projectReference: process.env.PROJECT,' '  implementationKind: "claude-code",' '  implementationDisplayName: "Claude Code",' '  workerKey: "laptop",' '  displayName: "Laptop",' '});' 'const beat = await client.heartbeatWorker(worker.id);' 'console.log(`${beat.display_name}: ${beat.status}`);' > worker.mjs
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Run it | node worker.mjs
check: curl https://api.veryfront.com/projects/$PROJECT/connected-agents -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "connection_status":"online"
```

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Register the worker with your [API key](/docs/authentication/api-keys.md):

```bash
curl -X POST https://api.veryfront.com/agent-workers/projects/$PROJECT/workers \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"implementation_kind": "claude-code", "implementation_display_name": "Claude Code", "worker_key": "laptop", "display_name": "Laptop"}'
```

3. Set `WORKER_ID` to the answer's `worker.id` and `WORKER_TOKEN` to its `token`, which it shows once:

```bash
export WORKER_ID=<worker.id>
export WORKER_TOKEN=<token>
```

4. Send a heartbeat with the worker's own token, so it stays online:

```bash
curl -X POST https://api.veryfront.com/agent-workers/workers/$WORKER_ID/heartbeat \
  -H "Authorization: Bearer $WORKER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

5. List the project's connected agents. You should now see the worker with `"connection_status": "online"`:

```bash
curl https://api.veryfront.com/projects/$PROJECT/connected-agents \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Register the worker | curl -X POST https://api.veryfront.com/agent-workers/projects/$PROJECT/workers -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"implementation_kind": "claude-code", "implementation_display_name": "Claude Code", "worker_key": "laptop", "display_name": "Laptop"}' | tee worker.json
step: Set WORKER_ID and WORKER_TOKEN | export WORKER_ID=$(jq -r .worker.id worker.json) WORKER_TOKEN=$(jq -r .token worker.json)
step: Send a heartbeat | curl -X POST https://api.veryfront.com/agent-workers/workers/$WORKER_ID/heartbeat -H "Authorization: Bearer $WORKER_TOKEN" -H "Content-Type: application/json" -d '{}'
check: curl https://api.veryfront.com/projects/$PROJECT/connected-agents -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "connection_status":"online"
```

## Next steps

* [Debug a run](/docs/observability/debug.md)
* [Connect a coding agent](/docs/mcp/connect.md)
