phonic
v0.12.0
Published
Phonic Node.js SDK
Downloads
754
Readme
Phonic Node.js SDK
Node.js library for the Phonic API.
Installation
npm i phonic
Setup
Grab an API key from the Phonic API Keys section and pass it to the Phonic constructor.
import { Phonic } from "phonic";
const phonic = new Phonic("ph_...");
Usage
Get voices
const { data, error } = await phonic.voices.list({ model: "shasta" });
if (error === null) {
console.log(data.voices);
}
Get voice by id
const { data, error } = await phonic.voices.get("meredith");
if (error === null) {
console.log(data.voice);
}
Get conversation by id
const { data, error } = await phonic.conversations.get("conv_b1804883-5be4-42fe-b1cf-aa84450d5c84");
if (error === null) {
console.log(data.conversation);
}
Get conversation by external id
const { data, error } = await phonic.conversations.getByExternalId("CAdb9c032c809fec7feb932ea4c96d71e1");
if (error === null) {
console.log(data.conversation);
}
Speech-to-speech via WebSocket
To start a conversation, open a WebSocket connection:
const { data, error } = await phonic.sts.websocket({
input_format: "mulaw_8000",
// Optional fields
system_prompt: "You are a helpful assistant.",
welcome_message: "Hello, how can I help you?",
voice_id: "meredith",
output_format: "mulaw_8000"
});
if (error !== null) {
throw new Error(`Failed to start conversation: ${error.message}`);
}
const { phonicWebSocket } = data;
Stream input (user) audio chunks:
phonicWebSocket.audioChunk({
audio: "...", // base64 encoded audio chunk
});
Process messages that Phonic sends back to you:
phonicWebSocket.onMessage((message) => {
switch (message.type) {
case "input_text": {
console.log(`User: ${message.text}`);
break;
}
case "audio_chunk": {
// Send the audio chunk to Twilio, for example:
twilioWebSocket.send(
JSON.stringify({
event: "media",
streamSid: "...",
media: {
payload: message.audio,
},
}),
);
break;
}
}
});
Update the system prompt mid-conversation:
phonicWebSocket.updateSystemPrompt({
systemPrompt: "..."
})
Set an external id for the conversation (can be the Twilio Call SID, for example):
phonicWebSocket.setExternalId({
externalId: "..."
})
To end the conversation, close the WebSocket:
phonicWebSocket.close();
You can also listen for close and error events:
phonicWebSocket.onClose((event) => {
console.log(
`Phonic WebSocket closed with code ${event.code} and reason "${event.reason}"`,
);
});
phonicWebSocket.onError((event) => {
console.log(`Error from Phonic WebSocket: ${event.message}`);
});
License
MIT