Make an Outbound Call

👍

You're ready for this how-to guide if you've got the following:

A FreeClimb account
A registered application
A configured FreeClimb Number
Your tools and language installed
Trial accounts: A verified number


Node.js

Using the FreeClimb SDK, you can create an asynchronous out dial request. Requests must include To, From, and callConnectUrl. Fill in To with the phone number you're calling (not provided in this example) and From with an incoming phone number you've purchased from FreeClimb. Successful invocation of the create method queues the asynchronous out dial request. Subsequent updates for the request are directed to the URLs provided.

Create your package.json file and save in the root directory of your project:

{
  "name": "node-make-an-outbound-call-how-to-guide",
  "version": "1.0.0",
  "license":"MIT",
  "scripts": {
    "start": "node makeACall.js"
  },
  "dependencies": {
    "@freeclimb/sdk": "^3.4.0",
    "body-parser": "^1.20.2",
    "dotenv": "^16.3.1",
    "express": "^4.18.2"
  }
}

Install the packages:

yarn install

Create an outbound call and respond to the receiver:

require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const freeclimbSDK = require('@freeclimb/sdk')
const { PerclScript, Say, MakeCallRequest } = freeclimbSDK

const app = express()
app.use(bodyParser.json())

const port = process.env.PORT || 3000
// your freeclimb API key (available in the Dashboard) - be sure to set up environment variables to store these values
const accountId = process.env.ACCOUNT_ID
const apiKey = process.env.API_KEY
const freeclimbConfig = freeclimbSDK.createConfiguration({ accountId, apiKey })
const apiInstance = new freeclimbSDK.DefaultApi(freeclimbConfig)

// Invoke create method to initiate the asynchronous outdial request
const applicationId = process.env.APPLICATION_ID
apiInstance.makeACall(new MakeCallRequest({
  _from: '', // Your FreeClimb number
  to: '', // Your verified phone number
  applicationId: applicationId,
})).catch(console.log)

// Handles incoming calls. Set with 'Call Connect URL' in App Config
app.post('/incomingCall', (req, res) => {
  const script = new PerclScript({
    commands: [new Say({ text: 'You just made a call with the FreeClimb API' })]
  })
  res.status(200).json(script.build())
})

app.listen(port, () => {
  console.log(`started the server on port ${port}`)
})

Run the example with the following command:

$ node makeACall.js

Java

After creating the FreeClimbClient, you can access the CallsRequester via the calls attribute to make an asynchronous out dial request. Requests must include To, From, and callConnectUrl. Fill in To with the phone number you're calling and From with an incoming phone number you've purchased from FreeClimb. A successful call of the create function queues the asynchronous out dial request. Subsequent updates for the request are directed to the Call Connect URL provided in the App Config in the FreeClimb dashboard.

Create your build.gradle file and save it to the root directory in your project:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds
 */
 
buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
   //Add the dependency
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE"
    }
}
 
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
 
bootJar {
    baseName = 'gs-spring-boot'
    version =  '0.1.0'
}
 
repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}
 
sourceCompatibility = 1.8
targetCompatibility = 1.8
 
dependencies {
    compile "org.springframework.boot:spring-boot-starter-web"
    testCompile "junit:junit"
    compile 'com.github.FreeClimbAPI:java-sdk:3.0.0'
}
 
sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }
}

Set up Spring boot:

package main.java.make_outbound_call;
 
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class Application {
 
  public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(Application.class, args);
 
    System.out.println("Let's inspect the beans provided by Spring Boot:");
 
    String[] beanNames = ctx.getBeanDefinitionNames();
    Arrays.sort(beanNames);
    for (String beanName : beanNames) {
      System.out.println(beanName);
    }
    MakeOutboundCallController.run();
  }
}

Make the outbound call and respond to the receiver:

package main.java.make_outbound_call;
 
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import com.vailsys.freeclimb.api.FreeClimbClient;
import com.vailsys.freeclimb.api.FreeClimbException;
import com.vailsys.freeclimb.percl.Say;
import com.vailsys.freeclimb.percl.PerCLScript;
import com.vailsys.freeclimb.api.call.Call;
 
@RestController
public class MakeOutboundCallController {
  private static FreeClimbClient client;
 
  public static void run() {
    String accountId = System.getenv("ACCOUNT_ID");
    String apiKey = System.getenv("API_KEY");
    String applicationId = System.getenv("APPLICATION_ID");
    String toNumber = System.getenv("TO_PHONE_NUMBER");
    String fromNumber = System.getenv("FREECLIMB_PHONE_NUMBER");
    try {
      client = new FreeClimbClient(accountId, apiKey);
    } catch (FreeClimbException e) {
      // handle exception
    }
 
    outDial(fromNumber, toNumber, applicationId);
  }
 
  public static void outDial(String fromNumber, String toNumber, String applicationId) {
    try {
      // Create FreeClimbClient object
      Call call = client.calls.create(toNumber, fromNumber, applicationId);
    } catch (FreeClimbException ex) {
      // Exception throw upon failure
      System.out.print(ex);
    }
  }
 
  // set url in call connect field in FreeClimb dashboard
  @RequestMapping("/InboundCall")
  public String respond() {
    PerCLScript script = new PerCLScript();
    Say say = new Say("You just made a call with the FreeClimb API!");
    // Add PerCL play script to PerCL container
    script.add(say);
    return script.toJson();
  }
}

Build and run the server with the command:

$ gradle build && java -Dserver.port=<Port Number> -jar build/libs/gs-spring-boot-0.1.0.jar

C#

After creating the FreeClimbClient, you can access the CallsRequester to make an asynchronous out dial request. Requests must include To, From, and applicationId. Fill in To with the phone number you're calling and From with an incoming phone number you've purchased from FreeClimb. A successful call of the create method queues the asynchronous out dial request. Subsequent updates for the request are directed to the application provided.

Install the SDK:

dotnet add package freeclimb-cs-sdk --version 2.0.0

Example code:

FreeClimbClient client = new FreeClimbClient (acctId, apiKey);
string to = "";
string from = "";
string appId = "";
client.getCallsRequester.create(to, from, appId );

The Call Connect Url, which is set in the App Config in the FreeClimb dashboard, will receive a request for PerCL from FreeClimb. Implement this endpoint with a controller with the following code:

using com.freeclimb;
using com.freeclimb.percl;
using com.freeclimb.webhooks.call;
using System;
using Microsoft.AspNetCore.Mvc;
 
namespace MakeOutboundCall.Controllers {
  [Route ("connect")]
  [ApiController]
  public class FreeClimbController : ControllerBase {
 
    [HttpPost]
    public ActionResult CallConnect (CallStatusCallback freeClimbRequest) {
        // Create an empty PerCL script container
        PerCLScript script = new PerCLScript ();
        Say say = new Say();
        say.setText("You just got called by the C sharp S D K!");
        Console.WriteLine(freeClimbRequest.getCallStatus);
        
        script.Add (say);
 
        // Convert PerCL container to JSON and append to response
        return Content (script.toJson (), "application/json");
    }
  }
}

Run the example:

$ dotnet run

Python

After creating the FreeClimb ApiClient object, you can use the make_a_call function in the DefaultApi class to make an out dial request. This API call requires a FreeClimb account ID and an instance of the FreeClimb MakeCallRequest model. MakeCallRequest requires the arguments to, from and application_id. Assign the to argument with the phone number you're calling and from argument with an incoming phone number you've purchased from FreeClimb. All subsequent updates for the request will be directed to the application provided.

Create your Requirements file and save in the root directory of your project:

urllib3 >= 1.15.1
certifi >= 14.05.14
six >= 1.10
python-dateutil >= 2.5.3
freeclimb >= 4.0.0
Flask==1.1.1

Install the packages:

$ pip install -r requirements.txt

Create an outbound call through the FreeClimb Python SDK:

from __future__ import print_function
import time
import freeclimb
from freeclimb.api import default_api
import os
import json
from flask import Flask, request


configuration = freeclimb.Configuration(
    # Defining host is optional and default to https://www.freeclimb.com/apiserver
    host     = "https://www.freeclimb.com/apiserver",
    # Configure HTTP basic authorization: fc
    username = os.environ['ACCOUNT_ID'],
    password = os.environ['API_KEY']
)

# Create an instance of the API class
api_instance = default_api.DefaultApi(freeclimb.ApiClient(configuration))

app = Flask(__name__)

# Triggered locally for convenience
@app.route('/sendCall', methods=['POST'])
def sendCall():
    if request.method == 'POST':
        call_request = freeclimb.MakeCallRequest(
            _from=YOUR_FREECLIMB_NUMBER, to=YOUR_VERIFIED_NUMBER, application_id=YOUR_APP_ID)
        api_instance.make_a_call(make_call_request=call_request)
        return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}

The Call Connect Url, which is set in the the FreeClimb dashboard, will receive a request for PerCL from FreeClimb. This example creates a PerCL script that uses Say and Pause from PerCL. Implement this endpoint with a controller with the following code:

# Specify this route with 'CALL CONNECT URL' in App Config
@app.route('/callConnect', methods=['POST'])
def callConnect():
    if request.method == 'POST':
        script = freeclimb.PerclScript(commands=[
            freeclimb.Say(text="Hello. Welcome to FreeClimb's outbound call tutorial."),
            freeclimb.Pause(length=1000),
            freeclimb.Say(text="Goodbye.")
        ])

        return script.to_json(), 200, {'ContentType': 'application/json'}

The Status Callback Url, which is set in the the FreeClimb dashboard, will receive a request for PerCL from FreeClimb when there is a status callback. Implement this endpoint with a controller with the following code:

# Specify this route with 'STATUS CALLBACK URL' in App Config
@app.route('/status', methods=['POST'])
def status():
    return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}

Run the example:

$ env FLASK_APP=make_outbound_call_tutorial.py flask run

A call can be triggered by hitting the sendCall route.