Call Auth. w/ Twilio - JavaScript

Pre-requisites

  1. First Orion Branded Communications agreement
  2. Access to First Orion Customer Portal
  3. Vetted and Approved Business
  4. Ability to originate phone calls from configured phone numbers in calling platform
  5. Understanding of current calling platform and environment to integrate required API

Generate First Orion API Keys

See the API Credentials Page for more information. API Credential Page

Example

Prepare

  1. Replace lines 16 and 17 with the Business API keys.
  2. Replace lines 37 and 38 with the caller and callee information.
  3. Replace lines 65 and 66 with Twilio API Credentials.
  4. Replace lines 71 and 72 with the caller and callee information.

JavaScript Script

Libraries needed

  • axios
// CallAuth.js

// Libraries
const axios = require('axios'); // Install with,  npm install axios

// Gets First Orion Auth Token
const getToken = async () => {
  
    try {
      const response = await axios.post(
        'https://api.firstorion.com/v1/auth',
        null,
        {
          headers: {
            'X-SERVICE':'auth',
            'X-API-KEY': 'your-api-key',
            'X-SECRET-KEY': 'your-secret-key',
            'Content-Type': 'application/json'
          }
        }
      );
      return response.data.token;
      
    } catch (error) {
      console.error('Error:', error);
      return {
        statusCode: error.response ? error.response.status : 500,
        body: JSON.stringify({ error: error.message })
      };
    }
}

// Create Call Authentication push to First Orion for Call Authentication
// Find out more at: https://developer.firstorion.com/firstorion-public/reference/callauthentication
const push_callauth = async (token) => {
    let data = JSON.stringify({
        "aNumber": '+15555555555',
        "bNumber": '+15554444444'
    });

    let config = {
        method: 'post',
        url: 'https://api.firstorion.com/exchange/v1/calls/push',
        headers: { 
            'Authorization': token, 
            'Content-Type': 'application/json'
        },
        data : data
    };

    axios.request(config)
    .then((response) => {
        console.log(JSON.stringify(response.data));
    })
    .catch((error) => {
        console.log(error);
    });
}

// Replace this Create Call function based on your platform
// Download the helper library from https://www.twilio.com/docs/node/install
// Set environment variables for your credentials
// Read more at http://twil.io/secure
const createCall = async () =>{
  const accountSid = "yourAccountSid";
  const authToken = "youAuthToken";
  const client = require("twilio")(accountSid, authToken);

  client.calls.create({
    url: "http://demo.twilio.com/docs/voice.xml",
    to: "+15555555555",
    from: "+15554444444",
  })
  .then(call => console.log(call.sid));
}


// Main function to invoke the Call Authentication push for Call Authentication.
const main = async () => {
    const token = await getToken();
    const push_callauth = await push_callauth(token);
		const twilioCall = await createCall()
    console.log("Call Auth Info: " + push_callauth)
		console.log("Twilio Call Info: " + twilioCall)
}

main()

Responses

Run the Python script in

fouser@FO-user-pc JavaScript % node CallAuth.js
Call Auth Info:
{
  "body": {
    "message": "Ok"
  }
}
Twilio Call Info:
{
  "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  ...
  ...
}