The official Python SDK for the NextGenSwitch Programmable Voice API. Create and modify calls with synchronous or asynchronous clients, build escaped Voice XML, stream audio to AI services, and parse Gather and Dial callbacks.
- Python 3.9 or newer
- A NextGenSwitch deployment and API credentials
pip install nextgenswitchUntil the first PyPI release, install from GitHub:
pip install "nextgenswitch @ git+https://github.com/nextgenswitch/nextgenswitch-python.git"import os
from nextgenswitch import Client
client = Client(
os.environ["NEXTGENSWITCH_BASE_URL"],
os.environ["NEXTGENSWITCH_AUTHORIZATION"],
os.environ["NEXTGENSWITCH_AUTHORIZATION_SECRET"],
)The SDK sends the documented X-Authorization and X-Authorization-Secret headers. Keep these values in environment variables or a secret manager and use HTTPS for remote deployments.
from nextgenswitch import VoiceResponse
flow = VoiceResponse().say("Welcome to NextGenSwitch.").gather(
action="https://example.com/gather",
method="POST",
numDigits=1,
timeout=10,
children=lambda gather: gather.say("Press one for sales."),
)
result = client.create_call(
"2001",
"1001",
response_xml=flow,
status_callback="https://example.com/call-status",
)
print(result.data)Use response_url="https://example.com/call-flow.xml" instead of response_xml to provide a hosted XML document. Exactly one response source is required.
updated = (
VoiceResponse()
.pause(2)
.say("Your call flow has been updated.")
.dial("1000", answerOnBridge=True)
)
client.modify_call("CALL-123", updated)import asyncio
import os
from nextgenswitch import AsyncClient, VoiceResponse
async def main() -> None:
async with AsyncClient(
os.environ["NEXTGENSWITCH_BASE_URL"],
os.environ["NEXTGENSWITCH_AUTHORIZATION"],
os.environ["NEXTGENSWITCH_AUTHORIZATION_SECRET"],
) as client:
result = await client.create_call(
"2001",
"1001",
response_xml=VoiceResponse().say("Hello from async Python."),
)
print(result.data)
asyncio.run(main())| XML verb | Python method |
|---|---|
<Say> |
say(text, **attributes) |
<Play> |
play(url, **attributes) |
<Gather> |
gather(children=..., **attributes) |
<Dial> |
dial(to, children=..., **attributes) |
<Record> |
record(**attributes) |
<Connect><Stream> |
stream(url, parameters=..., **attributes) |
<Hangup> |
hangup() |
<Pause> |
pause(seconds) |
<Redirect> |
redirect(url, method=...) |
<Bridge> |
bridge(call_id, bridge_after_establish=...) |
<Leave> |
leave() |
Python's XML library escapes text and attributes instead of concatenating untrusted XML strings.
flow = (
VoiceResponse()
.say("Please leave your message after the beep.")
.record(
action="https://example.com/recording",
method="POST",
timeout=5,
finishOnKey="#",
transcribe=True,
trim=True,
beep=True,
)
.hangup()
)
client.create_call("2001", "1001", response_xml=flow)flow = VoiceResponse().stream(
"wss://voice.example.com/session",
parameters={"session_id": "session-123", "tenant": "example"},
name="assistant-stream",
)Resolve AI-provider secrets on the WebSocket service. Never put provider API keys in Voice XML.
from nextgenswitch import GatherResult
gather = GatherResult.from_mapping(request.form)
if gather.digits == "1":
response = VoiceResponse().say("Connecting sales.").dial("1001")
else:
response = VoiceResponse().say("No valid selection received.").hangup()Use DialResult.from_mapping(payload) for documented Dial action fields. Callback signature verification is not documented by the current API; restrict endpoints, require TLS, validate expected fields, and add deployment-appropriate authentication.
ValidationError: invalid SDK inputApiError: non-2xx response, withstatus_codeandresponse_bodyTransportError: network or HTTP transport failureNextGenSwitchError: base SDK exception
Configure NEXTGENSWITCH_BASE_URL, NEXTGENSWITCH_AUTHORIZATION, and NEXTGENSWITCH_AUTHORIZATION_SECRET, then adapt:
All example.com URLs are placeholders for TLS endpoints you control.
python -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[dev]"
ruff check .
mypy src/nextgenswitch
pytest
python -m build
python -m twine check dist/*GitHub Actions validates Python 3.9–3.13.