const apiKey = process.env.DIALNEXA_API_KEY!;
const baseUrl = "https://api.dialnexa.com/v1";
async function request(path: string, init: RequestInit = {}) {
const response = await fetch(`${baseUrl}${path}`, {
...init,
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
...init.headers,
},
});
const body = await response.json();
if (!response.ok) throw new Error(`${response.status}: ${JSON.stringify(body)}`);
return body;
}
const { data: agent } = await request("/agents", {
method: "POST",
body: JSON.stringify({
title: "Appointment Assistant",
language_id: "LANGUAGE_ID",
voice_id: "VOICE_ID",
prompt_text: "Confirm {{customer_name}}'s appointment.",
}),
});
await request(`/agents/${agent.id}`, {
method: "PATCH",
body: JSON.stringify({ version_number: 1, is_published: true }),
});
const call = await request("/calls", {
method: "POST",
body: JSON.stringify({
agent_id: agent.id,
phone_number: "+919876543210",
metadata: { customer_name: "Priya", correlation_id: "crm-8421" },
}),
});
console.log(call.id);