japhet_07843
07/31/2025, 1:45 AMAI Evy
07/31/2025, 1:45 AMAI Evy
07/31/2025, 1:46 AMjavascript
// install the sdk: <https://docs.retellai.com/get-started/sdk>
import { Retell } from "retell-sdk";
import express, { Request, Response } from "express";
const app = express() ;
app.use(express.json() ) ;
app.post("/webhook", (req: Request, res: Response) => {
const {event, call} = req.body;
switch (event) {
case "call_started":
console.log("Call started event received", call.call_id) ;
break;
case "call_ended":
console.log("Call ended event received", call.call_id) ;
break;
case "call_analyzed":
console.log("Call analyzed event received", call.call_id) ;
break;
default:
console.log("Received an unknown event:", event) ;
}
// Acknowledge the receipt of the event
res.status(204) .send() ;
}) ;
## 2. Custom Function Endpoints
For integrating external APIs and custom logic, you can create custom function endpoints [(2) ]() . These endpoints receive POST requests with function parameters and return results to the LLM .
Here's how to verify requests are from Retell using Node.js:AI Evy
07/31/2025, 1:46 AMjavascript
import { Retell } from "retell-sdk";
this.app.post("/check-weather", async (req: Request, res: Response) => {
if (
!Retell.verify(
JSON.stringify(req.body) ,
process.env.RETELL_API_KEY,
req.headers["x-retell-signature"] as string,
)
) {
console.error("Invalid signature") ;
return;
}
const content = req.body;
if (content.args.city === "New York") {
return res.json("25f and sunny") ;
} else {
return res.json("20f and cloudy") ;
}
}) ;
## 3. WebSocket Endpoints for Custom LLM
For custom LLM integration, you can set up WebSocket endpoints [(3) ]() :
javascript
import { RawData, WebSocket } from "ws";
import { Request } from "express";
var express = require('express') ;
var app = express() ;
var expressWs = require('express-ws') (app) ;
const port = 3000
// Your other API endpoints
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.ws("/llm-websocket/:call_id",
async (ws: WebSocket, req: Request) => {
// callId is a unique identifier of a call, containing all information about it
const callId = req.params.call_id;
// You need to send the first message here, but for now let's skip that.
ws.on("error", (err) => {
console.error("Error received in LLM websocket client: ", err) ;
}) ;
ws.on("message", async (data: RawData, isBinary: boolean) => {
// Retell server will send transcript from caller along with other information
// You will be adding code to process and respond here
console.log(data) ;
}) ;
},
) ;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
}) ;
After setting up your endpoint locally, you can use ngrok to make it publicly accessible for testing [(1) ]() [(3) ]() .AI Evy
07/31/2025, 1:46 AMAI Evy
07/31/2025, 1:46 AM