Step By Step Basic Guideline To Use RetellAI API

Dikirimkan pada - Kali Terakhir Diubah Suai pada

Basic Guide To Use RetellAI API in Your Projects with Node.js or PHP

RetellAI is a powerful platform that allows developers to create automated phone calls and leverage AI-driven voice technology. With its flexible API, you can integrate these capabilities into your projects using popular programming languages like Node.js and PHP. This blog will guide you through the steps to use the RetellAI API effectively.

Why Use RetellAI API?

RetellAI API offers numerous features including:

  1. Automated phone calls powered by AI.
  2. Easy to use endpoints for creating and managing calls.
  3. Scalability and robust documentation.

By integrating the RetellAI API, you can enhance your applications with AI-driven voice capabilities for customer support, notifications, surveys, and more.

RetellAI API Request Types

The RetellAI API offers several request types including:

  1. Create Phone Call: Initiates an automated call with a message.
  2. Get Call Status: Retrieves the status of an ongoing or completed call.
  3. List Calls: Lists all calls for your account.
  4. Cancel Call: Cancels a scheduled or ongoing call.

This guide focuses on creating a phone call and retrieving its status. Check the documentation for more info.


Prerequisites

In this tutorial I'm using the example of Node & PHP however the API can be setup using Python, Java & Go as well but I'm not well versed in those.For more info please check the documentation:

https://docs.retellai.com/api-references/create-phone-call.

Before getting started, ensure you have the following:

  1. API Key: Sign up on RetellAI and retrieve your API key from the dashboard.
  2. Node.js or PHP Environment: Install Node.js or set up a PHP environment on your system.
  3. HTTP Client Library: Use libraries like Axios for Node.js or cURL for PHP to make API requests.

Step by Step Instructions for Node.js

Step 1: Install Axios

Create a folder, enter it and Install Axios, a popular HTTP client library, to send requests to the RetellAI API:

npm install axios
 

Step 2: Create a Phone Call

Please copy the code below and replace your API Key with the placeholder I have added. Save the file as createcall.js

const axios = require('axios');

const API_KEY = 'Enter_Your_Api_Key_Here'; 
const BASE_URL = 'https://api.retellai.com/v1';

const createPhoneCall = async () => {
  try {
    const response = await axios.post(
      `${BASE_URL}/phone-calls`,
      {
        phoneNumber: '+1234567890',
        message: 'Hello! This is an automated call from Laraib Rabbani using RetellAI.',
      },
      {
        headers: {
          'Authorization': `Bearer ${API_KEY}`,
          'Content-Type': 'application/json',
        },
      }
    );
    console.log('Phone call created:', response.data);
  } catch (error) {
    console.error('Error creating phone call:', error.response?.data || error.message);
  }
};

createPhoneCall();
 

Step 3: Retrieve Call Status

Please copy the code below to another file and save it as getCallStatus.php. Replace the call ID with your original call ID in the placeholder "Enter_your_call_id_here" and save again.

const getCallStatus = async (callId) => {
  try {
    const response = await axios.get(
      `${BASE_URL}/phone-calls/${callId}`,
      {
        headers: {
          'Authorization': `Bearer ${API_KEY}`,
        },
      }
    );
    console.log('Call status:', response.data);
  } catch (error) {
    console.error('Error retrieving call status:', error.response?.data || error.message);
  }
};

getCallStatus('Enter_your_call_id_here');
 

Step 4: Run the Code

Execute the code below in terminal.

node createCall.js
node getCallStatus.js

Step by Step Instructions for PHP

Step 1: Initialize cURL

In PHP, you can use cURL to interact with the RetellAI API. Ensure that the cURL extension is enabled in your PHP environment. Create a folder and name it anything you like.

Step 2: Create a Phone Call

Copy the code below and replace the Enter_Your_API_key_Here with your API Key save the code as createCall.php

<?php

$apiKey = 'Enter_Your_API_Key_Here';
$baseUrl = 'https://api.retellai.com/v1';

$data = [
    'phoneNumber' => '+1234567890',
    'message' => 'Hello! This is an automated call from Laraib using RetellAI.',
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "$baseUrl/phone-calls");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $apiKey,
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    echo 'Phone call created: ' . $response;
}

curl_close($ch);

?>
 

Step 3: Retrieve Call Status

To get the status of a specific call, copy the code below and save it as getCallStatus.php in the main folder you created in step 1 then replace the API and Call ID.

<?php

$apiKey = 'Enter_your_api_key_here';
$baseUrl = 'https://api.retellai.com/v1';
$callId = 'Enter_your_call_id_here';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "$baseUrl/phone-calls/$callId");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $apiKey,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    echo 'Call status: ' . $response;
}

curl_close($ch);

?>

Step 4: Run the Code

Execute the code below in the terminal:

php createCall.php
php getCallStatus.php

Notes:

  1. Follow Documentation: Refer to the RetellAI API documentation for detailed information on available endpoints and parameters

Dipaparkan 10 Januari, 2025

Brown Developers

🔷 Freelancer.com Verified & Preferred

Hi, This is Laraib Rabbani. Welcome to Brown Developers 🐻 & My Profile Page. We build partnerships. Let's work together and maximize your business potential. Our Expertise: ⚜️BACKEND Node, PHP, Java ⚜️CMS WordPress, Shopify, Wix, Squarespace, Webflow ⚜️Frontend: Nextjs, Reactjs, Html, CSS, Tailwind 📲Mobile Apps React native ⚜️SEO: Page Speed Optimization (Google Page Speed Insight 90+ Guar...

Artikel Seterusnya

Time Management Strategies for Freelancers: Maximizing Productivity