const SDK = require("@fonoster/sdk");const client = new SDK.Client({ accessKeyId: "WO000000000000000000000000000000" });async function makeCall() { const calls = new SDK.Calls(client); const breakReasons = ["ANSWER", "NOANSWER", "BUSY", "FAILED", "CANCEL"]; try { const { ref, statusStream } = await calls.createCall({ from: "+18287854037", to: "+17853178070", // Replace with your application reference appRef: "4b01c9b1-8cb1-48fb-bd49-f3daf13463a9", timeout: 30, // This will be available in the initial request sent to your application. metadata: { name: "John Doe", preferredLanguage: "en-US" } }); console.log(`Call created with reference: ${ref}`); // Monitor call status for await (const s of statusStream) { console.log(`Call status: ${s.status}`); if (breakReasons.includes(s.status)) { break; } } process.exit(0); } catch (err) { console.error("Error making call:", err); }}client.loginWithApiKey("AP0eerv2g7qow3e950k7twu4rvydcunq3k", "fNc...") .then(() => makeCall()) .catch(console.error);
When using the SDK, you can monitor call status through the statusStream:
const { ref, statusStream } = await calls.createCall({ from: "+18287854037", to: "+17853178070", appRef: "4b01c9b1-8cb1-48fb-bd49-f3daf13463a9"});// Monitor call status changesfor await (const status of statusStream) { switch(status) { case "RINGING": console.log("Call is ringing"); break; case "IN_PROGRESS": console.log("Call is connected"); break; case "COMPLETED": console.log("Call completed successfully"); break; case "FAILED": console.log("Call failed"); break; }}