phonic
v0.1.2
Published
Phonic Node.js SDK
Downloads
177
Maintainers
Readme
Phonic Node.js SDK
Node.js library for the Phonic API.
Installation
npm i phonic
Setup
Grab an API key from Phonic settings 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();
if (error === null) {
console.log(data.voices);
}
Get voice by id
const { data, error } = await phonic.voices.get("australian-man");
if (error === null) {
console.log(data.voice);
}
Text-to-speech via WebSocket
const { data, error } = await phonic.tts.websocket();
if (error === null) {
const { phonicWebSocket } = data;
const stream = phonicWebSocket.send({
script: "How can I help you today?", // 600 characters max
output_format: "mulaw_8000", // or "pcm_44100"
});
for await (const data of stream) {
if (data instanceof Buffer) {
// Do something with the audio chunk,
// e.g. send `data.toString("base64")` to Twilio.
}
}
phonicWebSocket.close();
}
To perform other work while receiving chunks, use:
phonicWebSocket.onMessage((data) => {
if (data instanceof Buffer) {
// Do something with the audio chunk,
// e.g. send `data.toString("base64")` to Twilio.
}
});
phonicWebSocket.send({
script: "How can I help you today?",
output_format: "mulaw_8000",
});
// Perform other work here
await phonicWebSocket.streamEnded; // This Promise will be resolved once the last chunk is received
License
MIT