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

# Voiceflow

> Capture Voiceflow agent conversations that run through the Dialog API

## Choose a setup method

| Method | Use it when |
| - | - |
| **Agnost AI skill** | You want your coding agent to inspect the app, make the change, and verify it. |
| **Agnost AI SDK** | You want explicit control over the interaction boundaries and fields. |
| **OpenTelemetry** | The framework already emits useful spans or you operate an OTel pipeline. |

Start with one method for a call path. Combining SDK tracking with framework
OpenTelemetry on the same call can create duplicate interactions.

## Recommended: Agnost AI skill

> [!NOTE]
>
> **Recommended.** Choose this path when you want your coding agent to inspect the
> project, select a supported transport, make the smallest instrumentation change,
> send a test event, and verify that it reached Agnost AI.

Install the skill once in your coding-agent environment:

```bash theme={null}
npx skills add AgnostAI/skills --skill agnost-ai
```

Run the framework-specific prompt below from the application root. Review the
resulting diff before deploying it.

```text theme={null}
Use the agnost-ai skill to add Agnost AI analytics to this Voiceflow integration.
Org ID: your-org-id
Wrap the real Voiceflow /v4/interact calls and verify one fresh conversation.
```

When your app talks to a Voiceflow agent through the Dialog API, your server sends each user message to `/v4/interact` and receives the agent's reply as traces. Wrap that call with the Agnost AI SDK to record one event per user message, with the agent's reply as the output.

## Manual setup: Agnost AI SDK

### Install

```bash theme={null}
pip install agnost requests
```

### Setup

Set `VOICEFLOW_API_KEY` to a Voiceflow API key with access to the project, and `VOICEFLOW_PROJECT_ID` to the project ID from the agent's settings. Keep the API key on your server.

```python theme={null}
import os
import uuid

import agnost
import requests

VOICEFLOW_URL = "https://general-runtime.voiceflow.com"
VOICEFLOW_API_KEY = os.environ["VOICEFLOW_API_KEY"]
VOICEFLOW_PROJECT_ID = os.environ["VOICEFLOW_PROJECT_ID"]

agnost.init("your-org-id")


def voiceflow(path: str, authorization: str, body: dict) -> dict:
    response = requests.post(
        f"{VOICEFLOW_URL}{path}",
        headers={"Authorization": authorization},
        json=body,
        timeout=60,
    )
    response.raise_for_status()
    return response.json()


def reply_text(traces: list) -> str:
    return "\n".join(
        trace["payload"]["message"]
        for trace in traces
        if trace["type"] in ("text", "speak") and (trace.get("payload") or {}).get("message")
    )


def start_conversation(user_id: str) -> dict:
    session = voiceflow(
        f"/v4/project/{VOICEFLOW_PROJECT_ID}/environment/main/session",
        VOICEFLOW_API_KEY,
        {"userID": user_id},
    )
    conversation = {
        "id": str(uuid.uuid4()),  # Agnost AI conversation ID for this session
        "user_id": user_id,
        "session_key": session["sessionKey"],
    }
    launch = voiceflow("/v4/interact", conversation["session_key"], {"action": {"type": "launch"}})
    conversation["greeting"] = reply_text(launch["traces"])
    return conversation


def send_message(conversation: dict, text: str) -> list:
    interaction = agnost.begin(
        user_id=conversation["user_id"],
        agent_name="voiceflow-agent",
        input=text,
        conversation_id=conversation["id"],
    )
    if interaction is None:
        raise RuntimeError("Agnost is not initialized. Call agnost.init() first.")
    try:
        turn = voiceflow(
            "/v4/interact",
            conversation["session_key"],
            {"action": {"type": "text", "payload": text}},
        )
        interaction.end(output=reply_text(turn["traces"]))
        return turn["traces"]
    except Exception as exc:
        interaction.end(output=str(exc), success=False)
        raise
```

Call `start_conversation()` when a customer opens a chat and store the result with that customer's chat state. Call `send_message()` for each message they send, and render the returned traces as you do today. `main` is the environment alias; use the alias of the environment you deploy to.

Call `agnost.shutdown()` once when the application exits, not after each request.
Shutting down per request disables telemetry for later messages in the same process.

### Data mapping

* The `id` you create per Voiceflow session becomes the Agnost AI conversation ID. Do not use the session key; it is a credential.
* `user_id` becomes the Agnost AI user ID. Use the same value you send to Voiceflow as `userID`.
* The user's text is the event input. Every `text` and `speak` trace's `payload.message` in the response, joined with newlines, is the output.
* An HTTP error, including Voiceflow's `402` when the account is out of credits, is recorded as a failed event with the error text.
* The `launch` greeting is not recorded, because the customer has not said anything yet.

## Why there is no OpenTelemetry section

Voiceflow runs your agent on its own servers and does not export OpenTelemetry traces. Voiceflow's session webhooks report when a conversation starts and ends, but they carry IDs, not the transcript, so record each turn where your code calls the Dialog API.

Conversations on Voiceflow's hosted chat widget and phone numbers do not pass through your code, so this setup does not capture them.

### Verify

1. Start a conversation and send two messages through your integration.
2. Open **Conversations** in [app.agnost.ai](https://app.agnost.ai). Both messages appear in one conversation for the user ID you passed.
3. Open an event and confirm the input is the user's message and the output is the agent's reply.

### Troubleshooting

* **Outputs are empty:** the agent answered with buttons, cards, or carousels only. `reply_text` reads `text` and `speak` traces. Add the trace types your agent uses.
* **Each message is its own conversation:** reuse the `conversation` returned by `start_conversation()` for later messages instead of starting a new session.
* **`401` from Voiceflow:** session creation needs your API key; `/v4/interact` needs the returned `sessionKey`.
* **Nothing appears in Agnost AI:** confirm `agnost.init()` runs once at startup with your organization ID, and that a short-lived script calls `agnost.shutdown()` before it exits.

### References

* [Voiceflow: start a session](https://www.voiceflow.com/docs/api-reference/v4interact/start-session-specific-environment)
* [Voiceflow: send actions](https://www.voiceflow.com/docs/api-reference/v4interact/interact-non-stream)
* [Voiceflow: handling responses](https://www.voiceflow.com/docs/api-reference/agent-runtime/chat/responses)
* [Voiceflow: session lifecycle webhook](https://www.voiceflow.com/docs/api-reference/webhooks/session-lifecycle)
* [Agnost AI Python SDK](https://docs.agnost.ai/python-conversation.md)

## Next steps

* [Conversations](https://docs.agnost.ai/using-conversations.md): review complete Voiceflow conversations.
* [Events](https://docs.agnost.ai/using-events.md): inspect each user message and the agent's reply.
* [Patterns](https://docs.agnost.ai/using-patterns.md): organize production conversations by what users wanted.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.