andreapalacio
02/25/2025, 7:06 PMAI Evy
02/25/2025, 7:06 PMAI Evy
02/25/2025, 7:07 PMjavascript
npm install retell-client-js-sdk
[(1)]()
Then set up the SDK class:
javascript
import { RetellWebClient } from "retell-client-js-sdk";
const retellWebClient = new RetellWebClient();
## Call create-web-call to get call ID
Your client code should call your server endpoint which internally calls the create web call API to get the access token for the call. This requires your Retell API key, which is why you need to call the endpoint from the server instead of the client (to protect your API key).
Note that if you don't start the call within 30 seconds of obtaining the access token, it will be invalidated and the call will be marked with an error.
According to community discussions, you should use the "create-call" endpoint instead of the older "register-call" endpoint. [(2)]()
## Start the Call
Once you have the access token, you can start the call:
javascript
await retellWebClient.startCall({
accessToken: createCallResponse.access_token,
});
[(1)]()
There are optional parameters you can include:AI Evy
02/25/2025, 7:07 PMjavascript
await retellWebClient.startCall({
accessToken: createCallResponse.access_token,
sampleRate: 24000, // (Optional) set sample rate of the audio capture and playback
// (Optional) device id of the mic.
captureDeviceId: "default",
// (Optional) device id of the speaker
playbackDeviceId: "0ec1807fd0fe6e51b990660ec4e2ebb78sdfcba51e279815d00c423ce03407ff",
// (Optional) Whether to emit "audio" events that contain raw pcm audio bytes represented by Float32Array
emitRawAudioSamples: false,
});
## Listen to Events
You can listen to events emitted by the SDK to get real-time updates about the call:
javascript
retellWebClient.on("call_started", () => {
console.log("call started");
});
retellWebClient.on("call_ended", () => {
console.log("call ended");
setIsCallActive(false);
});
// When agent starts talking for the utterance
retellWebClient.on("agent_start_talking", () => {
console.log("agent_start_talking");
});
// When agent is done talking for the utterance
retellWebClient.on("agent_stop_talking", () => {
console.log("agent_stop_talking");
});
// Update message such as transcript
retellWebClient.on("update", (update) => {
// console.log(update);
});
retellWebClient.on("metadata", (metadata) => {
// console.log(metadata);
});
retellWebClient.on("error", (error) => {
console.error("An error occurred:", error);
// Stop the call
retellWebClient.stopCall();
});
## Stop the Call
To end the call:
javascript
retellWebClient.stopCall();
**Note**: According to community discussions, there may be some discrepancies between the documentation and the current implementation. The most recent approach is to make the server-side request to "/create-call" rather than "/register-call". [(2)]()AI Evy
02/25/2025, 7:07 PM