Call forwarding API
The Call Forwarding API allows you to configure a destination for incoming calls to your Sonetel phone numbers.
This option is available regardless if your phone numbers are purchased via the API or via the Sonetel customer portal.
Via the Sonetel API you can configure the call forwarding of your phone number.
Optional call forwarding destinations
The API allows you to change so that incoming calls are forwarded to either of these types of destinations.
- Phone numbers
Forward incoming calls to any mobile number or landline worldwide. Call forwarding costs like a local call. - IVR
Calls can be forwarded to IVR-menus, voicemail services or announcements that you either create via the API or via the customer portal. - SIP-addresses
Incoming calls can be forwarded across the Internet to any SIP address of choice. - Team members
You can add team members in your Sonetel account. Each of them can have their own call forwarding settings, which will apply if you connect the number to them.
Changing call forwarding with Python
Sample code for changing call forwarding of a phone number with Python can be found here. The following code can be used to change the call forwarding.
from sre_constants import SUCCESS import requests import json base_url = "https://pubic-api.sonetel.com/" # API Access token access_token = "ENTER_ACCESS_TOKEN" #forward phone number fwd_phn_num ="ENTER PHONE NUMBER *DONT USE +*"; #Your phone number in the E164 format without the leading +. # Sonetel account ID acc_id = "ENTER_ACCOUNT_ID" #account information headers = {"Authorization": "Bearer {}".format(access_token), 'Content-Type': 'application/json' } payload_fwd = json.dumps({ "connect_to_type": "ENTER_TYPE", "connect_to": "ENTER_PARA" }) ########################forwared call#################### def forward_call(): response = requests.request("PUT", "{}account/{}/phonenumbersubscription/{}".format(base_url, acc_id, fwd_phn_num), headers=headers, data=payload_fwd).json() status = response["status"] if status=="success": return True else: return False if(forward_call()): print("call forwarded successfully")
Changing call forwarding with NodeJS
Sample code for changing call forwarding of a phone number with NodeJS can be found here.
Here is the sample code.
import http from "https"; const change_call_forwarding = (accessToken, e164Number, settings) => { const host = process.env.SonetelAPIName ?? "public-api.sonetel.com"; if (!accessToken || !accessToken instanceof String) { throw Error("accessToken is required and must be passed as a String"); } if (!e164Number || !e164Number instanceof String) { throw Error("e164Number is required and must be passed as a String"); } if ( !settings || !settings.hasOwnProperty("connect_to") || !settings.hasOwnProperty("connect_to_type") ) { throw Error( 'settings is required and must be passed as an Object with "connect_to" and "connect_to_type" properties.' ); } const parsedToken = JSON.parse( Buffer.from(accessToken.split(".")[1], "base64").toString() ); const accountId = parsedToken.acc_id; const options = { hostname: host, path: `/account/${accountId}/phonenumbersubscription/${e164Number}`, method: "PUT", headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}`, Accept: "application/json", }, }; const req = http.request(options, (res) => { const { statusCode } = res; if (statusCode !== 200) { throw new Error(`Request failed. Status code ${statusCode}`); } res.setEncoding("utf-8"); let rawData = ""; res.on("data", (chunk) => (rawData += chunk)); res.on("end", () => { try { const parsedData = JSON.parse(rawData); console.log(parsedData); } catch (e) { console.error(e.message); } }); }); req.on("error", (e) => { throw new Error(`Request failed. ${e.message}`); }); req.write(JSON.stringify(settings)); req.end(); }; export { change_call_forwarding };
The function accepts three arguments:
accessToken
: This is the Oauth access token generated using the /oauth/token endpoint.e164Number
: This is the phone number for which the call forwarding has to be changed. This must be a phone number you have subscribed to.settings
: The settings object contains the details of where the calls must be forwarded. It must have two properties:connect_to
, which specifies the phone number, SIP address or ID of the voice app where the incoming calls should be forwarded, andconnect_to_type
, which specifies the type of the entity specified inconnect_to
.
The function first checks that the accessToken
and e164Number
arguments are passed as strings, and that the settings
argument is an object with the required properties. It then parses the accessToken
to get the account ID of the user. It then sets up an HTTP request to the Sonetel API, using the specified hostname
(defaulting to “public-api.sonetel.com” if not provided), the account ID and the phone number, and the PUT
method to update the call forwarding settings. Finally, it sends the request and handles the response.