TypeScript SDK · @pyai/sdk v0.4.0
Bring natural voice to your TypeScript app.
Use typed requests and native ReadableStream audio in your Node.js backend.
From download to working audio
Node.js 22+. A PyAI key. The starter writes speech.pcm; run npm start -- call.wav to also transcribe a recording.
curl -fSL https://pyai.com/starters/pyai-typescript.tar.gz -o pyai-typescript.tar.gz
tar -xzf pyai-typescript.tar.gz
cd pyai-typescript
npm install
cp .env.example .env
# Fill in .env, then:
npm startEdit the downloaded .env before running. Keep it out of Git. On Windows, use WSL for these shell commands.
What happens when you run it?
The starter runs on Node.js. Keep secret keys on your backend; use the browser voice-agent guide for frontend authentication.
Take it into your application
Choose a voice from the API catalog and check its language coverage. Reuse clients, consume streaming audio immediately, and cancel interrupted responses. PCM, WAV and G.711 stream; MP3 and Opus are buffered.
The complete entry point
This is the same file included in the download. Copy it here, or get the full project with its dependency and environment files.
import { createWriteStream } from "node:fs";
import { readFile } from "node:fs/promises";
import { basename } from "node:path";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import PyAI from "@pyai/sdk";
const apiKey = process.env.PYAI_API_KEY;
if (!apiKey) throw new Error("Set PYAI_API_KEY in .env");
const client = new PyAI({ apiKey });
const started = performance.now();
let received = 0;
const stream = await client.audio.speechStream({
input: "Hello! Your PyAI voice integration is ready.",
voice: process.env.PYAI_VOICE || "stock_emma_en_gb",
response_format: "pcm", sample_rate: 24000,
});
async function* chunks() {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value.length && !received) {
console.log(`First audio bytes: ${Math.round(performance.now() - started)} ms`);
}
received += value.length;
yield value; // Forward to your live player here; pipeline preserves backpressure.
}
} finally {
await reader.cancel();
reader.releaseLock();
}
}
await pipeline(Readable.from(chunks()), createWriteStream("speech.pcm"));
if (!received) throw new Error("Speak returned no audio");
console.log(`Saved speech.pcm (${received} bytes; mono PCM16LE at 24000 Hz)`);
const recording = process.argv[2];
if (recording) {
const result = await client.audio.transcriptions.create({
file: new Blob([await readFile(recording)]), filename: basename(recording),
}); // Omit language for automatic detection.
console.log(result.text);
}
Build with TypeScript in your coding agent
Install Node.js 22+ and your coding agent first. Connect PyAI’s MCP server, then ask it to call get_started. For a sandbox test, ask it to call create_sandbox_key once. Keep live credentials in your environment.
codex mcp add pyai -- npx -y @pyai/mcp@0.2.0claude mcp add --transport stdio --scope project pyai -- npx -y @pyai/mcp@0.2.0{
"mcpServers": {
"pyai": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"@pyai/mcp@0.2.0"
]
}
}
}Merge the pyai entry into your existing mcpServers configuration. Enable it in Cursor’s MCP settings.
Read https://pyai.com/skill.md and https://pyai.com/sdks/typescript.md.
Use the matching starter to add PyAI voice to this project.
Keep credentials in environment variables. Run a short synthetic
audio test and report the result and any missing credentials.
Do not place a phone call or send a message unless I ask.Want a reusable local skill? Copy the Codex and Claude Code skill install commands. Any tool that reads Markdown can use skill.md.
TypeScript SDK questions
What is included in the download?
The TypeScript starter includes its complete entry point, pinned PyAI package version, dependency manifest, environment template, and run instructions.
What do I need before running it?
A PyAI key. The starter writes speech.pcm; run npm start -- call.wav to also transcribe a recording.
Can I use this with Cursor, Codex or Claude Code?
Yes. Connect the PyAI MCP server and give your agent the TypeScript Markdown guide. It includes the complete starter source and setup commands.
How do I test streaming performance?
Measure first received audio and playback readiness separately in your application. Network location, load, text, and voice affect timing. Playing the saved file does not measure streaming latency.