Search for a virtual number using Python
After searching for a number, rent and configure that number for SMS, Voice or both.
Note:
Before you can get started, you need the following already set up:
- Complete all Numbers API prerequisite steps.
- Python and a familiarity with how to create a new file.
- A virtual number you have confirmed to be available.
Rent and configure your virtual number
- Create a new file named
rent-config-number.py
and paste the provided code found on this page into the file.
Rent and configure a virtual number
import requests
project_id = "YOUR_projectId"
phone_number = "YOUR_phoneNumber"
url = "https://numbers.api.sinch.com/v1/projects/" + project_id + "/availableNumbers/" + phone_number + ":rent"
payload = {
"smsConfiguration": {
"servicePlanId": "YOUR_servicePlanId",
"scheduledProvisioning": {
"status": "WAITING",
"errorCodes": [
"INTERNAL_ERROR"
]
},
"campaignId": "YOUR_10DLC_campaignId"
},
"voiceConfiguration": {
"appId": "YOUR_appId",
"scheduledProvisioning": {
"status": "WAITING"
}
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers, auth=('YOUR_username','YOUR_password'))
data = response.json()
print(data)
Fill in your parameters
- Assign your values to the following parameters:
Parameter | Your value |
---|---|
YOUR_username | your client_id or key_id found in the Sinch Customer Dashboard. |
YOUR_password | Your client_secret or key_secret . This is generated upon new key creation in the Customer Dashboard. Generate a new key if you have lost your secret key. |
YOUR_servicePlanId | Your SMS service plan IDThis is only required for SMS configuration. |
YOUR_10DLC_campaignId | Your 10DLC campaign ID for this campaign. Found on the TCR platform. Remove this parameter entirely for non-10DLC numbers. |
YOUR_appId | The Voice app ID or voice API key . Found in the Customer Dashboard. |
YOUR_ProjectID | The project ID found in the Customer Dashboard. |
YOUR_PhoneNumber | The virtual phone number that you previously searched for and would like to rent. |
- Save the file.
Execute the code
- Execute the code to rent and simultaneously configure your virtual number. Open a command prompt or terminal to the location where your Python file is saved and run the following command:
python rent-config-number.py
Response
You should get a response similar to this one:
{
"phoneNumber": "+12025550134",
"projectId": "51bc3f40-f266-4ca8-8938-a1ed0ff32b9a",
"displayName": "string",
"regionCode": "US",
"type": "MOBILE",
"capability": [
"SMS"
],
"money": {
"currencyCode": "USD",
"amount": "2.00"
},
"paymentIntervalMonths": 0,
"nextChargeDate": "2019-08-24T14:15:22Z",
"expireAt": "2019-08-24T14:15:22Z",
"smsConfiguration": {
"servicePlanId": "82b42acf74924bd687ef9fb212f2060c",
"scheduledProvisioning": {
"servicePlanId": "82b42acf74924bd687ef9fb212f20611",
"status": "WAITING",
"lastUpdatedTime": "2019-08-24T14:15:22Z",
"campaignId": "string",
"errorCodes": [
"INTERNAL_ERROR"
]
},
"campaignId": "string"
},
"voiceConfiguration": {
"appId": "string",
"scheduledVoiceProvisioning": {
"appId": "string",
"status": "WAITING",
"lastUpdatedTime": "2019-08-24T14:15:22Z"
},
"lastUpdatedTime": "2019-08-24T14:15:22Z"
}
}
Next steps
Send a message to yourself using the SMS API or the Voice API (after setting each up accordingly) to verify that the configuration was successful.
Additional resources
- Explore the API specification to test more endpoints.
- Follow the number rental process in the Customer Dashboard UI rather than through this API.