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

# Build and publish an agent from code

> Create a Voiceflow project, configure its agent and Playbook, publish an Environment, then start a session and receive a chat reply using the API or CLI.

Build a small support agent in a new project, then send it a customer message. Use the Platform API or CLI to create and publish resources; use the Chat session API to run the published agent.

## Before you begin

- A Voiceflow account with permission to create a project in a workspace, edit its agent and Playbooks, publish its Environment and start a conversation. The workspace needs credits to run the agent.
- A [personal API key](/docs/api-reference/authentication#personal-keys). From the workspace dashboard, open **Settings → Personal keys → New key** and follow the key-creation procedure. Set `VF_TOKEN` from your secret store. Keep the key on your server or development machine.
- Bash with cURL and jq. For the CLI alternative, [install `vf`](/docs/cli/install).

These examples use Voiceflow’s default public-cloud API bases:

```bash
set -euo pipefail
: "${VF_TOKEN:?Set VF_TOKEN to your personal API key}"
export VF_API_BASE='https://realtime-api.voiceflow.com'
export VF_RUNTIME_BASE='https://general-runtime.voiceflow.com'
```

The following workflow creates and publishes a **new test project**. Use its returned project ID throughout; do not substitute a production project. The example replaces the new agent’s routing with one Playbook.

## Create and publish the agent

Run the commands in the same Bash session. Choose one complete management workflow. Both leave `VF_PROJECT_ID` and `VF_ENVIRONMENT` set for the shared conversation steps below.

#### API

### 1. Choose a workspace

```bash
curl --fail-with-body --silent --show-error \
  "$VF_API_BASE/v1/stable/workspace" \
  --header "Authorization: Bearer $VF_TOKEN" > workspaces.json
jq '.workspaces[] | {id, name}' workspaces.json
export VF_WORKSPACE_ID='YOUR_CHOSEN_WORKSPACE_ID'
```

Choose an ID returned under `workspaces`. With a personal key, this list represents account membership; appearing in it does not prove the key can create or edit every resource there. See [workspace discovery and permissions](/docs/api-reference/authentication#permissions).

### 2. Create a test project and find its Environment

```bash
jq -n --arg workspaceID "$VF_WORKSPACE_ID" \
  '{name:"Support quickstart", workspaceID:$workspaceID, type:"webchat"}' > project.json
curl --fail-with-body --silent --show-error \
  --request POST "$VF_API_BASE/v1/stable/project" \
  --header "Authorization: Bearer $VF_TOKEN" \
  --header 'Content-Type: application/json' \
  --data-binary @project.json > project-created.json
VF_PROJECT_ID="$(jq -er '.project.id' project-created.json)"

curl --fail-with-body --silent --show-error --get \
  "$VF_API_BASE/v1/stable/environment" \
  --header "Authorization: Bearer $VF_TOKEN" \
  --data-urlencode "projectID=$VF_PROJECT_ID" > environments.json
VF_ENVIRONMENT="$(jq -er '.environments[] | select(.isMain) | .alias' environments.json)"
export VF_PROJECT_ID VF_ENVIRONMENT
```

The project response wraps the ID in `project`. Read the main Environment’s alias from the returned list rather than guessing its display name.

### 3. Create a Playbook and register it with the agent

```bash
jq -n '{name:"Order help", description:"Answer order-support questions", instructions:"Ask for the order number. Explain that this test agent cannot look up real orders. Offer to connect the customer to support."}' > playbook.json
curl --fail-with-body --silent --show-error --request POST \
  "$VF_API_BASE/v2/stable/playbook?projectID=$VF_PROJECT_ID&environmentAlias=$VF_ENVIRONMENT" \
  --header "Authorization: Bearer $VF_TOKEN" \
  --header 'Content-Type: application/json' \
  --data-binary @playbook.json > playbook-created.json
VF_PLAYBOOK_ID="$(jq -er '.playbook.id' playbook-created.json)"
jq -n --arg id "$VF_PLAYBOOK_ID" \
  '{prompt:"You are a support agent. Use the Order help Playbook for order questions. Do not invent order status.", playbooks:[{playbookID:$id, description:"Help with an order"}], workflows:[]}' > agent.json
curl --fail-with-body --silent --show-error --request PATCH \
  "$VF_API_BASE/v2/stable/agent?projectID=$VF_PROJECT_ID&environmentAlias=$VF_ENVIRONMENT" \
  --header "Authorization: Bearer $VF_TOKEN" \
  --header 'Content-Type: application/json' \
  --data-binary @agent.json
```

These agent and Playbook operations use the current `/v2/stable` contract, where `instructions` is Markdown text. Creating a Playbook does not make it reachable: `playbooks` registers it with the agent. That array replaces the existing registry; this example is for the new test project only.

### 4. Publish the Environment

```bash
curl --fail-with-body --silent --show-error --request POST \
  "$VF_API_BASE/v1/stable/environment/$VF_ENVIRONMENT/publish?projectID=$VF_PROJECT_ID" \
  --header "Authorization: Bearer $VF_TOKEN" \
  --header 'Content-Type: application/json' \
  --data-binary '{"name":"Support quickstart"}' > release.json
jq -e '.release.name == "Support quickstart"' release.json
```

Publishing creates a named release from this Environment’s draft. Check the returned release before starting the published session.

#### CLI

The CLI reads the same `VF_TOKEN`. Keep `--server-url` explicit so every management command targets the selected API base.

### 1. Choose a workspace

```bash
vf workspace list --server-url "$VF_API_BASE" --output-format json > workspaces.json
jq '.workspaces[] | {id, name}' workspaces.json
export VF_WORKSPACE_ID='YOUR_CHOSEN_WORKSPACE_ID'
```

Select a returned ID where your personal key permits project creation. The list describes membership, not a guarantee of write access.

### 2. Create a test project and find its Environment

```bash
vf project create --name 'Support quickstart' --type webchat \
  --workspace-id "$VF_WORKSPACE_ID" --server-url "$VF_API_BASE" \
  --output-format json > project-created.json
VF_PROJECT_ID="$(jq -er '.project.id' project-created.json)"
vf environment list --project-id "$VF_PROJECT_ID" \
  --server-url "$VF_API_BASE" --output-format json > environments.json
VF_ENVIRONMENT="$(jq -er '.environments[] | select(.isMain) | .alias' environments.json)"
export VF_PROJECT_ID VF_ENVIRONMENT
```

### 3. Create a Playbook and register it with the agent

```bash
vf playbook create --project-id "$VF_PROJECT_ID" \
  --environment-alias "$VF_ENVIRONMENT" --name 'Order help' \
  --description 'Answer order-support questions' \
  --instructions 'Ask for the order number. Explain that this test agent cannot look up real orders. Offer to connect the customer to support.' \
  --server-url "$VF_API_BASE" --output-format json > playbook-created.json
VF_PLAYBOOK_ID="$(jq -er '.playbook.id' playbook-created.json)"
jq -n --arg id "$VF_PLAYBOOK_ID" \
  '{prompt:"You are a support agent. Use the Order help Playbook for order questions. Do not invent order status.", playbooks:[{playbookID:$id, description:"Help with an order"}], workflows:[]}' > agent.json
vf agent update --project-id "$VF_PROJECT_ID" \
  --environment-alias "$VF_ENVIRONMENT" --body "$(cat agent.json)" \
  --server-url "$VF_API_BASE" --output-format json
```

The Playbook must appear in the agent’s routing registry. The update replaces that registry, so use it only in the new test project.

### 4. Publish the Environment

```bash
vf environment publish --project-id "$VF_PROJECT_ID" \
  --environment-alias "$VF_ENVIRONMENT" --name 'Support quickstart' \
  --server-url "$VF_API_BASE" --output-format json > release.json
jq -e '.release.name == "Support quickstart"' release.json
```

## Start a session and send a message

Both management alternatives continue here. Use the Chat session API for the conversation, rather than a Platform API management command.

### 1. Create a published Chat session

```bash
jq -n --arg userID "quickstart-$(date +%s)" \
  '{userID:$userID, versionVariant:"published"}' > session.json
curl --fail-with-body --silent --show-error --request POST \
  "$VF_RUNTIME_BASE/v4/project/$VF_PROJECT_ID/environment/$VF_ENVIRONMENT/session" \
  --header "Authorization: $VF_TOKEN" \
  --header 'Content-Type: application/json' \
  --data-binary @session.json > session-created.json
VF_SESSION_KEY="$(jq -er '.sessionKey' session-created.json)"
```

The returned `sessionKey` belongs to this conversation. Keep it for both actions below; do not create another session between turns.

### 2. Launch the agent

```bash
curl --fail-with-body --silent --show-error --request POST \
  "$VF_RUNTIME_BASE/v4/interact" \
  --header "Authorization: $VF_SESSION_KEY" \
  --header 'Content-Type: application/json' \
  --data-binary '{"action":{"type":"launch"}}' > launch.json
jq '.traces' launch.json
```

Action requests use the session key directly, without a Bearer prefix. The personal key used for management does not replace it.

### 3. Send an order-support message

```bash
jq -n --arg message 'Can you help with my order?' \
  '{action:{type:"text",payload:$message}}' > message.json
curl --fail-with-body --silent --show-error --request POST \
  "$VF_RUNTIME_BASE/v4/interact" \
  --header "Authorization: $VF_SESSION_KEY" \
  --header 'Content-Type: application/json' \
  --data-binary @message.json > reply.json
jq -e '[.traces[] | select(.type == "text")] | length > 0' reply.json
jq '.traces[] | select(.type == "text") | .payload' reply.json
```

JSON encoding through jq preserves quotes and line breaks in customer messages. For incremental output in an application, use [HTTP Streaming](/docs/api-reference/v4interact/interact-non-stream); this quickstart uses completed-turn JSON to make the result easy to inspect.

## Verify the result

Confirm a release was returned, the session returned a non-empty `sessionKey`, and the customer message returned at least one `text` trace. Read the reply: it should follow the Order help instructions and avoid inventing an order status. A successful HTTP status alone does not establish that the agent followed the intended Playbook.

The new project and published release remain in your workspace. Keep them for further development, or remove the test project deliberately through [project management](/docs/api-reference/resources/project) when finished.

## Troubleshooting

| Symptom | Next check |
| --- | --- |
| A workspace is listed but a write is denied | Check the personal key’s resource/action scopes and your account’s access. Membership discovery does not grant a write. |
| The agent does not use the Playbook | Verify the saved agent’s `playbooks` registry includes its returned ID in the same Environment. |
| Published-session startup fails | Confirm publishing succeeded for this project and Environment; do not substitute the draft or another project’s alias. |
| An action is unauthorized | Use the returned session key directly in `Authorization`, without `Bearer`, and reuse the same session. |
| A turn fails or returns only diagnostic traces | Inspect the response, credits and current [error reference](/docs/api-reference/agent-runtime/chat/recovery). |

## Related tasks

- [Authentication](/docs/api-reference/authentication)
- [Platform API reference](/docs/api-reference/api-overview)
- [Chat sessions](/docs/api-reference/v4interact/start-session-specific-environment)
- [Handling Chat responses](/docs/api-reference/agent-runtime/chat/responses)
