Twilio Voice API - Call Authentication
Pre-requisites
- First Orion Branded Communications agreement
- Access to First Orion Customer Portal
- Vetted and Approved Business
- Ability to originate phone calls from configured phone numbers in calling platform
- 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
- Sign into First Orion's Customer Portal
- Navigate to the API Keys section on the left-hand side
- Click Generate Key
- Copy and save or download the generated keys
Examples
Python Example
Libraries needed - json, requests, os
- Replace lines 13 and 14 with the Business API keys.
- Replace lines 26 and 27 with the caller and callee information.
- Replace lines 54 and 55 with Twilio API Credentials.
- Replace lines 60 and 61 with the caller and callee information.
JavaScript Example
Libraries needed - axios
- Replace lines 16 and 17 with the Business API keys.
- Replace lines 37 and 38 with the caller and callee information.
- Replace lines 65 and 66 with Twilio API Credentials.
- Replace lines 71 and 72 with the caller and callee information.
# CallAuth.py
# Libraries
import json
import requests
import os
def get_token():
url = "https://api.firstorion.com/v1/auth"
headers = {
'X-SERVICE': 'auth',
'content-type': 'application/json',
'X-API-KEY': 'your-api-key',
'X-SECRET-KEY': 'your-secret-key'
}
response = requests.request("POST", url, headers=headers)
data = response.json()
return data['token']
def push_precall(token):
url = "https://api.firstorion.com/exchange/v1/calls/push"
payload = json.dumps({
"aNumber": "+15555555555",
"bNumber": "+15554444444"
})
headers = {
'Authorization': token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
data = response.json()
value = {
'statusCode': response.status_code,
'body': data['body']['message']
}
# Return JSON Object
return json.loads(json.dumps(value))
# Replace this Create Call function based on your platform
# Download the helper library from https://www.twilio.com/docs/python/install
# Set environment variables for your credentials
# Read more at http://twil.io/secure
import os
from twilio.rest import Client
def createCall():
account_sid = "your_account_sid"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)
call = client.calls.create(
url="http://demo.twilio.com/docs/voice.xml",
to="+15555555555",
from_="+15554444444"
)
print(call.sid)
# Makes the pre-call push.
token = get_token()
precall = push_precall(token)
createTwilioCall = createCall()
print("Call Auth Info: " + precall)
print("Twilio Call Info: "+ createTwilioCall)// 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 example script.
fouser@FO-user-pc Python % python3 CallAuth.py
Call Auth Info:
{
"body": {
"message": "Ok"
}
}
Twilio Call Info:
{
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
...
...
}Updated about 18 hours ago