How to Create a Voice Agent From Code in Python
Build the agent with a few method calls, save it as a draft, ring your own phone, then publish when it sounds right

To create a voice agent from code, install a platform SDK, give it an API key, describe the agent as nodes joined by edges, save it, and place a test call to your own phone. With the open-source SDKs from Dograh, the open-source AI voice agent platform, that's pip install dograh-sdk for Python 3.10 or newer, or npm install @dograh/sdk for TypeScript, and about twenty lines for a working three-step agent.
The thing worth knowing before you write a line is that a save never goes straight to callers. Every save from code becomes a draft, and the published version keeps answering live calls until someone clicks Publish. That one rule shapes the whole workflow below.
Why a platform SDK and not a framework
There are two kinds of voice agent SDK. A framework SDK puts your process inside the call, so you run the audio loop yourself and wire it to a carrier. A platform SDK configures agents that the platform runs. Your code creates the agent, saves it, starts calls and reads results, while the platform runs the pipeline and keeps the call history.
The second kind suits a team that wants agents in version control, or generated from its own data, without running audio servers. The agent also stays visible in the dashboard, so a teammate who doesn't code can open it and tweak a prompt.
Set up the key and the address
In the dashboard, open Developers in the sidebar, click Create New API Key, name it and click Create Key. The key starts with dgr_ and appears exactly once, so put it straight into your secrets store.
Both SDKs read two environment variables.
export DOGRAH_API_URL=https://api.dograh.com
export DOGRAH_API_KEY=dgr_...
The client adds /api/v1 to that address and sends the key as an X-API-Key header on every request. On a self-hosted install, point DOGRAH_API_URL at your own server instead. On the server itself the SDK's default, http://localhost:8000, already works, and from anywhere else you use the public HTTPS address. A quick client.list_workflows() confirms the key works by listing your agents with their IDs.
Create an empty agent to save into
The SDK saves onto an agent that already exists, so make one in the dashboard first. Open Voice Agents, click Create Agent and pick Blank Canvas, since every save from code replaces the agent's nodes and edges anyway. The ID is the number at the end of the editor's address, so /workflow/123 means agent 123.
Describe the agent in code
The SDK mirrors the canvas. add() creates a node, edge() joins two of them with a condition, and save_workflow writes the result. This sample is the loan qualification agent from the SDK docs.
from dograh_sdk import DograhClient, Workflow
with DograhClient(api_key="YOUR_API_KEY") as client:
wf = Workflow(client=client, name="loan_qualification")
greeting = wf.add(
type="startCall",
name="greeting",
prompt="You are Sarah from Acme Loans. Greet the caller warmly.",
)
qualify = wf.add(
type="agentNode",
name="qualify",
prompt="Ask about loan amount, purpose, and monthly income.",
)
done = wf.add(
type="endCall",
name="done",
prompt="Thank them and end the call politely.",
)
wf.edge(greeting, qualify, label="interested", condition="Caller wants to continue.")
wf.edge(qualify, done, label="done", condition="All qualification questions answered.")
client.save_workflow(workflow_id=123, workflow=wf)
Each edge's condition is plain text telling the agent when to move on. In TypeScript it's the same agent with await on each add, an options object on each edge and await client.saveWorkflow(123, wf). Both packages are generated from the same backend OpenAPI spec, so the methods match one for one, snake_case in Python and camelCase in TypeScript.
The part we like most is the checking. The SDK fetches each node type's spec from the backend as you call add(), and it stops an unknown field such as a typo, a missing required field, a value of the wrong type, or an option outside the allowed list before anything is saved. The server checks again on save. client.list_node_types() shows which node types and fields exist.
To change an existing agent, load it, edit it and save again, which writes another draft.
wf = client.load_workflow(workflow_id=123)
wf.name = "loan_qualification_v2"
client.save_workflow(workflow_id=123, workflow=wf)
Ring your own phone with the draft
A test call runs the draft, so you hear your changes before any caller does. You'll need a phone provider such as Twilio, Telnyx or Plivo connected under Telephony first.
from dograh_sdk import DograhClient
from dograh_sdk._generated_models import InitiateCallRequest
with DograhClient(api_key="YOUR_API_KEY") as client:
client.test_phone_call(
body=InitiateCallRequest(
workflow_id=123,
phone_number="+14155550123",
)
)
Swap in your own mobile with + and the country code. Don't skip this step in favor of a browser test. Our standing view is that most phone calls still reach the agent as 8 kHz audio, while a browser demo runs at 16 kHz, so speech recognition and the voice can behave differently on a real handset. For many calls at once, use Campaigns rather than looping over test calls, because campaigns handle pacing and retries.
Once the call sounds right, open the agent in the dashboard and click Publish.
Read what happened
Every call creates a run with its status, transcript, recording link and the details the agent gathered. The SDKs cover building and calling, and runs come from the Runs API with the same key.
curl -H "X-API-Key: dgr_..." https://api.dograh.com/api/v1/workflow/123/runs
curl -H "X-API-Key: dgr_..." https://api.dograh.com/api/v1/workflow/123/runs/456
The first lists every run of agent 123, and the second returns one run in full, including recording_url and transcript_url.
Before real traffic
Live calls should go to the published agent through its API trigger, not through test calls. Keep the key on your server, never in browser or mobile code, and use one key per environment so a leaked staging key can't touch production agents. Both SDKs live in the main repository under the BSD 2-Clause license, so you can read the code before depending on it. On Dograh Cloud the platform runs for you, while a self-hosted install puts the same API on your own servers.
The exact dashboard steps and the TypeScript version of every sample are in our walkthrough of the Python and TypeScript voice agent SDKs.
About the author
- Building Dograh (https://dograh.com) - an open-source alternative to Vapi. Love for OSS and Voice AI
Comments
Join the discussionNo comments yet. Be the first to comment.





