*If you want to use the content of this page with an AI tool, or if you are an AI agent, use the Markdown version of this page: `https://developers.sinch.com/docs/numbers/getting-started.md`* *You may also connect to our documentation MCP server by clicking the **Connect MCP** button: * # Getting started This guide will instruct you on how to get started using the Sinch Numbers API. The steps below will walk you through account creation and help you gather the information you'll need to configure the Numbers API for SMS, Voice, or both. Let's get started! Account required! Using the Numbers API requires [signing up for a free account](https://community.sinch.com/t5/Customer-Dashboard/How-to-sign-up-for-your-free-Sinch-account/ta-p/8058) on the Sinch Build Dashboard. If you haven't already done so, [sign up now](https://dashboard.sinch.com/signup)! ## 1. Get your Project ID and credentials **Get your project ID and create your access key:** First, you'll need your project ID, which can be found in the [Sinch Build Dashboard](https://dashboard.sinch.com/settings/project-settings) under **Setting**, then **Projects**. Then, create your access key. Access keys are used to authenticate calls when using Numbers API. Access keys are generated in the [Sinch Build Dashboard](https://dashboard.sinch.com/settings/access-keys). You'll need this info later! Make sure you have your access key, access key secret, and project ID readily available. This information will be required when making the API call to send the text message. The access key and access key secret must be recorded during access key creation. The project ID can be found on the Sinch Build Dashboard's [Project Settings](https://dashboard.sinch.com/settings/project-settings) page. Important! The client ID will be readily visible. The secret ID, however, will only be visible upon new key creation. Create a new key and then save your secret ID in a safe place. You will need both the client ID and the client secret for most endpoints in the Numbers API. ## 2. Get a number Now, you're ready to buy your first number. There are two ways you can buy a number: - Buy the first available number based on provided criteria - Search for an available number and then buy that number. Select one of the tabs below to see the method you want to use. Buy first available number The payload, along with instructions on how to populate placeholder variables, is below. Alternatively, each tab features a guide that will walk you through setting up a simple application in the identified coding language (make sure you bring all the information you gathered during this process!): Payload ```json Payload { "regionCode": "YOUR_region_code", "type": "YOUR_type", "capabilities": [ "SMS" ], "smsConfiguration": { "servicePlanId": "YOUR_SMS_servicePlanId", "campaignId": "YOUR_campaignId_from_TCR" } } ``` This payload will rent the first available number that fits the search criteria you specified and assign (or provision) it to your SMS service plan ID. The placeholder values included in the payload above are detailed in the table below: Since this guide is intended for testing purposes, you can use your access key and access secret to authenticate using [basic authentication](/docs/numbers/api-reference/authentication/basic), but for production we recommend using a stronger method of authentication, like [OAuth 2.0](/docs/numbers/api-reference/authentication/oauth). | Parameter | Your value | | --- | --- | | `YOUR_region_code` | The two letter abbreviation of the country for which you'd like a number. For example, the United States is `US`. Should be in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-2) format. | | `YOUR_type` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. | | `YOUR_SMS_servicePlanId` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest). This is required for SMS configuration. | | `YOUR_campaignId_from_TCR` | Only for US phone numbers. This `campaignId` is required to send SMS traffic to US; click [here](https://community.sinch.com/t5/10DLC/What-is-10DLC/ta-p/7845) to read more about 10DLC A2P messaging. So, it is the current campaign ID for this number. The `campaignId` is found on your TCR platform. | Node.js ### Rent the first available number using the Node.js SDK Use this guide to set up your Node.js application for use with the Numbers API and rent the first available Sinch virtual number and assign it to your SMS service plan. Note: Before you can get started, you need the following already set up: - [NPM](https://www.npmjs.com/) and a familiarity with how to install packages. - [Node.js](https://nodejs.org/en/) and a familiarity with how to create a new app. #### Set up your Node.js application To quickly get started setting up a simple client application using the Node SDK: 1. If you haven't already, clone the [sinch-sdk-node-quickstart](https://github.com/sinch/sinch-sdk-node-quickstart) repository. 2. Navigate to the `templates/client` folder. 3. Open a command prompt or terminal and run the following command to install the necessary dependencies: ```shell npm install ``` 4. Open the `.env` [file](https://github.com/sinch/sinch-sdk-node-quickstart/blob/main/templates/client/.env). Using the [access key credentials](https://dashboard.sinch.com/settings/access-keys) from your Sinch Build Dashboard, populate the following fields with your values: | Field | Description | | --- | --- | | SINCH_PROJECT_ID | The unique ID of your Project. | | SINCH_KEY_ID | The unique ID of your access key. | | SINCH_KEY_SECRET | The secret that goes with your access key. **Note:** For security reasons, this secret is only visible right after access key creation. | If you're sending SMS messages, ensure you set your region in the `SMS_REGION` field. 5. Save the file. 1. Navigate to the `/templates/client/src/numbers/` folder and open the `snippet.js` file. Replace the existing content within that file with the `snippet.js` code provided on this page. That code is also found [here](https://github.com/sinch/sinch-sdk-node-snippets/blob/main/snippets/numbers/available-numbers/rent-any/snippet.js) if you want to just replace the file. snippet.js //This code rents the first available number and configures it for use. // eslint-disable-next-line no-unused-vars import { Numbers, NumbersService } from '@sinch/sdk-core'; /** @param {NumbersService} numbersService */ export const execute = async (numbersService) => { const servicePlanId = 'YOUR_service_plan_id'; const appId = 'YOUR_app_id'; /** @type {Numbers.RentAnyNumberRequestSmsConfiguration} */ const smsConfiguration = { servicePlanId, }; /** @type {Numbers.RentAnyNumberRequestVoiceConfiguration} */ const voiceConfiguration = { appId, }; /** @type {Numbers.RentAnyNumberRequestData} */ const requestData = { rentAnyNumberRequestBody: { regionCode: 'US', type: 'LOCAL', numberPattern: { pattern: '+1781', searchPattern: 'START', }, smsConfiguration, voiceConfiguration, }, }; const response = await numbersService.availableNumber.rentAny(requestData); console.log(`Rented number:\n${JSON.stringify(response, null, 2)}`); }; 1. The code provided in **snippet.js** includes default parameters. If you want, you can replace the following values for these parameters with your own values: | Parameter | Your value | | --- | --- | | `regionCode` | The two letter abbreviation of the country for which you'd like a number. For example, the United States is `US`. Should be in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-2) format. | | `type` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. | | `servicePlanId` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest). This is required for SMS configuration. | | `appId` | Your [Voice app ID](https://dashboard.sinch.com/voice/apps). This is required for Voice configuration. | 1. Save the file. ### Run the code to rent the first available number using the Node.js SDK Now you can run the code with the following command: ```Shell node src/app.js ``` This code will rent the first available number that fits the search criteria you specified and assign (or provision) it to your SMS service plan ID. Java ### Rent the first available number using the Java SDK Use this guide to setup your Java application for use with the Numbers API and rent the first available Sinch virtual number and assign it to your SMS service plan. Note: Before you can get started, you need the following already set up: - [JDK 8 or later](https://www.oracle.com/java/technologies/downloads/) and a familiarity with how to create a new Java application. - [Apache Maven](https://maven.apache.org/install.html) and a familiarity with how to use the Maven CLI. #### Set up your Java application To quickly get started setting up a simple client application using the Java SDK: 1. If you haven't already, clone the [sinch-sdk-java](https://github.com/sinch/sinch-sdk-java) repository. 2. Navigate to the `examples/getting-started/numbers/rent-first-available-number/src/main/resources` folder. 3. Open the `config.properties` [file](https://github.com/sinch/sinch-sdk-java/blob/main/examples/getting-started/numbers/rent-first-available-number/src/main/resources/config.properties). Using the [access key credentials](https://dashboard.sinch.com/settings/access-keys) from your Sinch Build Dashboard, populate the following fields with your values: | Field | Description | | --- | --- | | SINCH_PROJECT_ID | The unique ID of your Project. | | SINCH_KEY_ID | The unique ID of your access key. | | SINCH_KEY_SECRET | The secret that goes with your access key. **Note:** For security reasons, this secret is only visible right after access key creation. | 1. Save the file. 2. Navigate to the `examples/getting-started/numbers/rent-first-available-number/src/main/java/numbers` folder and open the `Snippet.java` [file](https://github.com/sinch/sinch-sdk-java/blob/main/examples/getting-started/numbers/rent-first-available-number/src/main/java/numbers/Snippet.java). Snippet.java // This code rents the first available number and configures it for use. /** * Sinch Java Snippet * *

This snippet is available at https://github.com/sinch/sinch-sdk-java * *

See https://github.com/sinch/sinch-sdk-java/blob/main/examples/snippets/README.md for details */ package numbers; import com.sinch.sdk.SinchClient; import com.sinch.sdk.domains.numbers.api.v1.NumbersService; import com.sinch.sdk.domains.numbers.models.v1.ActiveNumber; import com.sinch.sdk.domains.numbers.models.v1.NumberType; import com.sinch.sdk.domains.numbers.models.v1.SmsConfiguration; import com.sinch.sdk.domains.numbers.models.v1.request.AvailableNumberRentAnyRequest; import com.sinch.sdk.models.Configuration; import java.util.logging.Logger; import utils.Settings; public class RentAny { private static final Logger LOGGER = Logger.getLogger(RentAny.class.getName()); public static void main(String[] args) { String projectId = Settings.getProjectId().orElse("MY_PROJECT_ID"); String keyId = Settings.getKeyId().orElse("MY_KEY_ID"); String keySecret = Settings.getKeySecret().orElse("MY_KEY_SECRET"); String servicePlanIdToAssociateWithTheNumber = Settings.getServicePlanId().orElse("MY_SERVICE_PLAN_ID"); // ISO 3166-1 alpha-2 country code of the phone number. e.g. "US", "GB", "SE"... // See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for details String regionCode = "MY_REGION_CODE"; NumberType numberType = NumberType.LOCAL; Configuration configuration = Configuration.builder() .setProjectId(projectId) .setKeyId(keyId) .setKeySecret(keySecret) .build(); SinchClient client = new SinchClient(configuration); NumbersService numbersService = client.numbers().v1(); SmsConfiguration smsConfiguration = SmsConfiguration.builder().setServicePlanId(servicePlanIdToAssociateWithTheNumber).build(); AvailableNumberRentAnyRequest parameters = AvailableNumberRentAnyRequest.builder() .setType(numberType) .setRegionCode(regionCode) .setSmsConfiguration(smsConfiguration) .build(); ActiveNumber response = numbersService.rentAny(parameters); LOGGER.info(String.format("Rented number: %s", response)); } } 1. The code provided in **Snippet.java** includes default and placeholder parameters. Replace the following following values for these parameters with your own values: | Parameter | Your value | | --- | --- | | `YOUR_region_code` | The two letter abbreviation of the country for which you'd like a number. For example, the United States is `US`. Should be in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-2) format. | | `YOUR_service_plan_id` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest). This is required for SMS configuration. | If you want, you can change the values of these enums to their other options: | Parameter | Your value | | --- | --- | | `Capability.SMS` | The capability of the number you would like to rent. Available options are `SMS` and `VOICE`. | | `NumberType.LOCAL` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. For more information, see the [JavaDocs](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/models/v1/NumberType.html). | 1. Save the file. 2. Now that you've modified the application, you need to use the Maven CLI to create a package that you can then execute. Open a command prompt or terminal to the `examples/getting-started/numbers/rent-first-available-number` folder and run the following command: ```Shell mvn package ``` This command creates the `target` folder and application. ### Run the code to rent the first available number using the Java SDK Now you can run the code with the following command: ```Shell java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar ``` This code will rent the first available number that fits the search criteria you specified and assign (or provision) it to your SMS service plan ID. You'll see the response in the console. Python ### Rent the first available number using the Python SDK Use this guide to setup your Python application for use with the Numbers API to rent the first available Sinch virtual number and provision it with your SMS service plan. Note: Before you can get started, you need the following already set up: - [Python](https://www.python.org/) and a familiarity with how to create a new file. - [PIP (package installer for Python)](https://pypi.org/project/pip/) and a familiarity with how to install Python modules. #### Installing the Python SDK The easiest way to install the SDK is using [`pip`](https://pypi.org/project/pip/): 1. Open a command prompt or terminal to the local repository folder. 2. Execute the following command: ```shell pip install sinch ``` #### Set up your Python application 1. Create a new file named `rent-any-number.py` and paste the provided code found on this page into the file. rent-any-number.py from sinch import SinchClient sinch_client = SinchClient( key_id="YOUR_key_id", key_secret="YOUR_key_secret", project_id="YOUR_project_id" ) activate_number_response = sinch_client.numbers.available.rent_any( region_code="YOUR_region_code", type_="YOUR_number_type", sms_configuration={ "servicePlanId": "YOUR_service_plan_id" } ) print(activate_number_response) 1. The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values. ##### Initialize the client Before initializing a client using this SDK, you'll need three pieces of information: - Your Project ID - An access key ID - An access key Secret These values can be found on the [**Access Keys**](https://dashboard.sinch.com/settings/access-keys) page of the Sinch Build Dashboard. You can also [create new access key IDs and Secrets](https://community.sinch.com/t5/Conversation-API/How-to-get-your-access-key-for-Conversation-API/ta-p/8120), if required. Note If you have trouble accessing the above link, ensure that you have gained access to the [Conversation API](https://dashboard.sinch.com/convapi/overview) by accepting the corresponding terms and conditions. ##### Fill in remaining parameters 1. Assign your values to the following required parameters: | Parameter | Your value | | --- | --- | | `YOUR_region_code` | The two letter abbreviation of the country for which you'd like a number. [ISO 3166–1 alpha–2. ](https://en.wikipedia.org/wiki/ISO_3166-2) | | `YOUR_number_type` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. | | `YOUR_service-plan-id` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest). This is required for SMS configuration. | 1. Save the file. #### Run the code to rent the first available number using the Python SDK Now you can run the code to rent the first available number. Open a command prompt or terminal to the location where your Python file is saved and run the following command: ```shell python rent-any-number.py ``` Search for a number The cURL commands, along with instructions on how to populate placeholder variables, is below. Alternatively, each tab features a guide that will walk you through setting up a simple application in the identified coding language (make sure you bring all the information you gathered during this process!): cURL ```shell cURL command curl -i -X GET \ -u : \ 'https://numbers.api.sinch.com/v1/projects/YOUR_projectId/availableNumbers?regionCode=US&type=MOBILE&capabilities=SMS' ``` This command searches for available mobile numbers in the US that have SMS capabilities. Once you have decided which available phone number you want to buy, you can buy it with the following cURL command: ```shell cURL command curl -i -X POST \ -u : \ 'https://numbers.api.sinch.com/v1/projects/YOUR_projectId/availableNumbers/YOUR_phone_number_to_buy:rent' \ -H 'Content-Type: application/json' \ -d '{ "smsConfiguration": { "servicePlanId": "YOUR_SMS_servicePlanId" } }' ``` Since this guide is intended for testing purposes, you can use your access key and access secret to authenticate using [basic authentication](/docs/numbers/api-reference/authentication/basic), but for production we recommend using a stronger method of authentication, like [OAuth 2.0](/docs/numbers/api-reference/authentication/oauth). The placeholder values included in the commands above are detailed in the table below: | Placeholder value | Your value | | --- | --- | | YOUR_access_key | This is your access key that you created in a previous step. | | YOUR_access_secret | This is your access secret that you created in a previous step. | | YOUR_phone_number_to_buy | This is one of the phone numbers returned in your search that you want to buy. | | YOUR_SMS_servicePlanId | This is your SMS service plan ID that you want to use with sending SMS messages. | Node.js ### Search for a virtual number using Node.js SDK Use this guide to create a Node.js application for use with the Numbers API and search for an available Sinch virtual number. Note: Before you can get started, you need the following already set up: - [NPM](https://www.npmjs.com/) and a familiarity with how to install packages. - [Node.js](https://nodejs.org/en/) and a familiarity with how to create a new app. #### Set up your Node.js application To quickly get started setting up a simple client application using the Node SDK: 1. If you haven't already, clone the [sinch-sdk-node-quickstart](https://github.com/sinch/sinch-sdk-node-quickstart) repository. 2. Navigate to the `templates/client` folder. 3. Open a command prompt or terminal and run the following command to install the necessary dependencies: ```shell npm install ``` 4. Open the `.env` [file](https://github.com/sinch/sinch-sdk-node-quickstart/blob/main/templates/client/.env). Using the [access key credentials](https://dashboard.sinch.com/settings/access-keys) from your Sinch Build Dashboard, populate the following fields with your values: | Field | Description | | --- | --- | | SINCH_PROJECT_ID | The unique ID of your Project. | | SINCH_KEY_ID | The unique ID of your access key. | | SINCH_KEY_SECRET | The secret that goes with your access key. **Note:** For security reasons, this secret is only visible right after access key creation. | If you're sending SMS messages, ensure you set your region in the `SMS_REGION` field. 5. Save the file. 1. Navigate to the `/templates/client/src/numbers/` folder and open the `snippet.js` file. Replace the existing content within that file with the `snippet.js` code provided on this page. That code is also found [here](https://github.com/sinch/sinch-sdk-node-snippets/blob/main/snippets/numbers/available-numbers/search-for-available-numbers/snippet.js) if you want to just replace the file. snippet.js //This code lists the available numbers for a given set of search criteria. // eslint-disable-next-line no-unused-vars import { Numbers, NumbersService } from '@sinch/sdk-core'; /** @param {NumbersService} numbersService */ export const execute = async (numbersService) => { /** @type {Numbers.ListAvailableNumbersRequestData} */ const requestData = { regionCode: 'US', type: 'LOCAL', capabilities: ['SMS', 'VOICE'], }; const response = await numbersService.availableNumber.list(requestData); console.log(`Available numbers to rent:\n${JSON.stringify(response, null, 2)}`); }; 1. The code provided in **snippet.js** includes default parameters. If you want, you can replace the following values for these parameters with your own values: | Parameter | Your value | | --- | --- | | `regionCode` | The two letter abbreviation of the country for which you'd like a number. For example, the United States is `US`. Should be in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-2) format. | | `type` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. | 1. Save the file. #### Run the code to search for numbers using the Node.js SDK Now you can execute the code. Run the following command: ```shell node src/app.js ``` You should receive a response in the console with a list of numbers available to rent. ### Buy an available number using the Node.js SDK Now that you have searched for an available number, you can rent and configure it for use with SMS and Voice. 1. Navigate to the `/templates/client/src/numbers/` folder and open the `snippet.js` file. Replace the existing content within that file with the `snippet.js` code provided on this page. That code is also found [here](https://github.com/sinch/sinch-sdk-node-snippets/blob/main/snippets/numbers/available-numbers/rent/snippet.js) if you want to just replace the file. snippet.js //This code rents and configures a specific virtual number. // eslint-disable-next-line no-unused-vars import { Numbers, NumbersService } from '@sinch/sdk-core'; /** @param {NumbersService} numbersService */ export const execute = async (numbersService) => { // Available numbers list can be retrieved by using list() function from available service, see: // https://developers.sinch.com/docs/numbers/getting-started/node-sdk/searchavailable/ const phoneNumber = 'available_phone_number_to_be_rented'; const servicePlanId = 'YOUR_service_plan_id'; /** @type {Numbers.RentNumberRequestData} */ const requestData = { phoneNumber, rentNumberRequestBody: { smsConfiguration: { servicePlanId, }, }, }; const response = await numbersService.availableNumber.rent(requestData); console.log(`Rented number:\n${JSON.stringify(response, null, 2)}`); }; 1. The code provided in **snippet.js** includes default parameters. If you want, you can replace the following values for these parameters with your own values: | Parameter | Your value | | --- | --- | | `servicePlanId` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest). This is only required for SMS configuration. | | `phoneNumber` | The virtual phone number that you previously searched for and would like to rent. | 1. Save the file. ### Run the code to buy a number using the Node.js SDK Now you can execute the code. Run the following command: ```shell node src/app.js ``` You should get a response similar to this one: ```json { "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" } } ``` Java ### Search for virtual number using the Java SDK Use this guide to setup your Java application for use with the Numbers API and search for an available Sinch virtual number. Note: Before you can get started, you need the following already set up: - [JDK 8 or later](https://www.oracle.com/java/technologies/downloads/) and a familiarity with how to create a new Java application. - [Apache Maven](https://maven.apache.org/install.html) and a familiarity with how to use the Maven CLI. #### Set up your Java application To quickly get started setting up a simple client application using the Java SDK: 1. If you haven't already, clone the [sinch-sdk-java](https://github.com/sinch/sinch-sdk-java) repository. 2. Navigate to the `examples/getting-started/numbers/search-available/client/src/main/resources` folder. 3. Open the `config.properties` [file](https://github.com/sinch/sinch-sdk-java/blob/main/examples/getting-started/numbers/search-available/src/main/resources/config.properties). Using the [access key credentials](https://dashboard.sinch.com/settings/access-keys) from your Sinch Build Dashboard, populate the following fields with your values: | Field | Description | | --- | --- | | SINCH_PROJECT_ID | The unique ID of your Project. | | SINCH_KEY_ID | The unique ID of your access key. | | SINCH_KEY_SECRET | The secret that goes with your access key. **Note:** For security reasons, this secret is only visible right after access key creation. | 1. Save the file. 2. Navigate to the `examples/getting-started/numbers/search-available/src/main/java/numbers` folder and open the `Snippet.java` [file](https://github.com/sinch/sinch-sdk-java/blob/main/examples/getting-started/numbers/search-available/src/main/java/numbers/Snippet.java). Snippet.java // This code returns a list of all the available numbers for a given set of search criteria. /** * Sinch Java Snippet * *

This snippet is available at https://github.com/sinch/sinch-sdk-java * *

See https://github.com/sinch/sinch-sdk-java/blob/main/examples/snippets/README.md for details */ package numbers; import com.sinch.sdk.SinchClient; import com.sinch.sdk.domains.numbers.api.v1.NumbersService; import com.sinch.sdk.domains.numbers.models.v1.NumberType; import com.sinch.sdk.domains.numbers.models.v1.request.AvailableNumbersListQueryParameters; import com.sinch.sdk.domains.numbers.models.v1.response.AvailableNumberListResponse; import com.sinch.sdk.models.Configuration; import java.util.logging.Logger; import utils.Settings; public class SearchForAvailableNumbers { private static final Logger LOGGER = Logger.getLogger(SearchForAvailableNumbers.class.getName()); public static void main(String[] args) { String projectId = Settings.getProjectId().orElse("MY_PROJECT_ID"); String keyId = Settings.getKeyId().orElse("MY_KEY_ID"); String keySecret = Settings.getKeySecret().orElse("MY_KEY_SECRET"); // ISO 3166-1 alpha-2 country code of the phone number. e.g. "US", "GB", "SE"... // See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for details String regionCode = "MY_REGION_CODE"; NumberType type = NumberType.LOCAL; Configuration configuration = Configuration.builder() .setProjectId(projectId) .setKeyId(keyId) .setKeySecret(keySecret) .build(); SinchClient client = new SinchClient(configuration); NumbersService numbersService = client.numbers().v1(); AvailableNumbersListQueryParameters parameters = AvailableNumbersListQueryParameters.builder() .setRegionCode(regionCode) .setType(type) .build(); LOGGER.info("Looking for available numbers"); AvailableNumberListResponse response = numbersService.searchForAvailableNumbers(parameters); response .iterator() .forEachRemaining( number -> LOGGER.info(String.format("Available number details: %s", number))); } } 1. The code provided in **Snippet.java** includes default parameters. If you want, you can replace the following values for these parameters with your own values: | Parameter | Your value | | --- | --- | | `regionCode` | The two letter abbreviation of the country for which you'd like a number. For example, the United States is `US`. Should be in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-2) format. | | `NumberType.LOCAL` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. For more information, see the [JavaDocs](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/models/v1/NumberType.html). | 1. Save the file. 2. Now that you've modified the application, you need to use the Maven CLI to create a package that you can then execute. Open a command prompt or terminal to the `examples/getting-started/numbers/search-available` folder and run the following command: ```Shell mvn package ``` This command creates the `target` folder and application. #### Run the code to search for numbers using the Java SDK Now you can run the code with the following command: ```Shell java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar ``` These steps should return a list of numbers available to rent for the search criteria you specified. ### Rent and configure your virtual number using the Java SDK After searching for a number, rent and configure that number for SMS. 1. Navigate to the `examples/getting-started/numbers/rent-and-configure/src/main/java/numbers` folder and open the `Snippet.java` [file](https://github.com/sinch/sinch-sdk-java/blob/main/examples/getting-started/numbers/rent-and-configure/src/main/java/numbers/Snippet.java). Snippet.java // Use this code to rent and configure a virtual number with the Java SDK. /** * Sinch Java Snippet * *

This snippet is available at https://github.com/sinch/sinch-sdk-java * *

See https://github.com/sinch/sinch-sdk-java/blob/main/examples/snippets/README.md for details */ package numbers; import com.sinch.sdk.SinchClient; import com.sinch.sdk.domains.numbers.api.v1.NumbersService; import com.sinch.sdk.domains.numbers.models.v1.ActiveNumber; import com.sinch.sdk.domains.numbers.models.v1.SmsConfiguration; import com.sinch.sdk.domains.numbers.models.v1.request.AvailableNumberRentRequest; import com.sinch.sdk.models.Configuration; import java.util.logging.Logger; import utils.Settings; public class Rent { private static final Logger LOGGER = Logger.getLogger(Rent.class.getName()); public static void main(String[] args) { String projectId = Settings.getProjectId().orElse("MY_PROJECT_ID"); String keyId = Settings.getKeyId().orElse("MY_KEY_ID"); String keySecret = Settings.getKeySecret().orElse("MY_KEY_SECRET"); String servicePlanIdToAssociateWithTheNumber = Settings.getServicePlanId().orElse("MY_SERVICE_PLAN_ID"); // Available numbers list can be retrieved by using list() function from available service, see // the SearchAvailableSnippet or // https://developers.sinch.com/docs/numbers/getting-started/java-sdk/searchavailable String phoneNumberToBeRented = "AVAILABLE_PHONE_NUMBER_TO_BE_RENTED"; Configuration configuration = Configuration.builder() .setProjectId(projectId) .setKeyId(keyId) .setKeySecret(keySecret) .build(); SinchClient client = new SinchClient(configuration); NumbersService numbersService = client.numbers().v1(); SmsConfiguration smsConfiguration = SmsConfiguration.builder().setServicePlanId(servicePlanIdToAssociateWithTheNumber).build(); AvailableNumberRentRequest parameters = AvailableNumberRentRequest.builder().setSmsConfiguration(smsConfiguration).build(); ActiveNumber response = numbersService.rent(phoneNumberToBeRented, parameters); LOGGER.info(String.format("Rented number: %s", response)); } } 1. The code provided in **Snippet.java** includes placeholder parameters. Replace the following values for these parameters with your own values: | Parameter | Your value | | --- | --- | | `YOUR_service_plan_id` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest)This is only required for SMS configuration. | | `YOUR_phone_number` | The virtual phone number that you previously searched for and would like to rent. | 1. Save the file. Now that you've modified the application, you need to use the Maven CLI to create a package that you can then execute. Open a command prompt or terminal to the `examples/getting-started/numbers/rent-and-configure` folder and run the following command: ```Shell mvn package ``` This command creates the `target` folder and application. #### Run the code to rent a number using the Java SDK Now you can run the code with the following command: ```Shell java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar ``` You should get a response similar to this one: ```json { "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" } } ``` .NET ### Search for a virtual number using .NET SDK Use this guide to create a .NET application for use with the Numbers API and search for an available Sinch virtual number. Note: - [.NET 7.0 SDK](https://dotnet.microsoft.com/download) or later and a familiarity with how to create a new app. #### Set up your .NET application 1. Create a new folder where you want your app project. Then, open a terminal or command prompt to that location. 2. Create a new .NET console app with the following command: ```shell dotnet new console ``` The easiest way to install the SDK is using the [`dotnet` CLI](https://learn.microsoft.com/en-us/dotnet/core/install/windows?tabs=net70): 1. Open a command prompt or terminal to the local repository folder. 2. Execute the following command: ```shell dotnet add package Sinch ``` 1. Open the `Program.cs` file in your project folder. Replace all of the code with the "Search for a virtual number" code. Program.cs // Use this code to search for available virtual numbers with the .NET SDK. using System.Text.Json; using Sinch; using Sinch.Numbers; using Sinch.Numbers.Available.List; var sinch = new SinchClient("YOUR_project_id", "YOUR_access_key", "YOUR_access_secret"); var response = await sinch.Numbers.Available.List(new ListAvailableNumbersRequest { RegionCode = "US", Type = Types.Local }); Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions() { WriteIndented = true })); 1. The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values. #### Initialize the client in .NET SDK Before initializing a client using this SDK, you'll need three pieces of information: - Your Project ID - An access key ID - An access key Secret These values can be found on the [**Access Keys**](https://dashboard.sinch.com/settings/access-keys) page of the Sinch Build Dashboard. You can also [create new access key IDs and Secrets](https://community.sinch.com/t5/Conversation-API/How-to-get-your-access-key-for-Conversation-API/ta-p/8120), if required. Note If you have trouble accessing the above link, ensure that you have gained access to the [Conversation API](https://dashboard.sinch.com/convapi/overview) by accepting the corresponding terms and conditions. To start using the SDK, you need to initialize the main client class with your credentials from your Sinch [dashboard](https://dashboard.sinch.com). Note: For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials. ```cpp Initialize client using Sinch; var sinch = new SinchClient("YOUR_project_id", "YOUR_access_key", "YOUR_access_secret"); ``` You can also implement the client using ASP.NET dependency injection. `SinchClient` is thread safe, so it's fine to add it as a singleton: ```cpp Initialize client using dependency injection builder.Services.AddSingleton(x => new SinchClient( builder.Configuration["YOUR_project_id"], builder.Configuration["YOUR_access_key"], builder.Configuration["YOUR_access_secret"])); ``` 1. When finished, save the file. ### Build your .NET project and run the code to search for numbers 1. Before executing your code, you must first compile your application. Execute the following command: ```shell dotnet build ``` 1. Now you can execute the code. Run the following command: ```shell dotnet run ``` ### Rent and configure your virtual number in .NET Now that you have searched for a number, you can rent and configure that number for SMS, Voice or both. 1. Open the `Program.cs` from your Search Available Numbers .NET project. Program.cs // Use this code to rent and configure a virtual number with the .NET SDK. using System.Text.Json; using Sinch; using Sinch.Numbers; using Sinch.Numbers.Available.Rent; var sinch = new SinchClient("YOUR_project_id", "YOUR_access_key", "YOUR_access_secret"); var response = await sinch.Numbers.Available.Rent("YOUR_phone_number", new RentActiveNumberRequest() { SmsConfiguration = new SmsConfiguration { ServicePlanId = "YOUR_service_plan_id", }, VoiceConfiguration = new VoiceConfiguration { AppId = "YOUR_app_id" } }); Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions() { WriteIndented = true })); 1. Paste the provided code in place of the previous code in the `Program.cs` file. 2. The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values. Replace following parameter values on your `Program.cs` file: | Parameter | Your value | | --- | --- | | `YOUR_service_plan_id` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest)This is only required for SMS configuration. | | `YOUR_app_id` | The Voice app ID or voice API `key`. Found in the [Sinch Build Dashboard.](https://dashboard.sinch.com/voice/apps) | | `YOUR_phone_number` | The virtual phone number that you previously searched for and would like to rent. | 1. Save the file. ### Build and run your .NET project 1. Before executing your code, you must first compile your application. Execute the following command: ```shell dotnet build ``` 1. Now you can execute the code. Run the following command: ```shell dotnet run ``` 1. You should get a response similar to this one: ```json { "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" } } ``` Python ### Search for a virtual number using Python Use this guide to setup your Python application for use with the Numbers API to search for an available Sinch virtual number. Note: Before you can get started, you need the following already set up: - [Python](https://www.python.org/) and a familiarity with how to create a new file. - [PIP (package installer for Python)](https://pypi.org/project/pip/) and a familiarity with how to install Python modules. #### Installing the SDK The easiest way to install the SDK is using [`pip`](https://pypi.org/project/pip/): 1. Open a command prompt or terminal to the local repository folder. 2. Execute the following command: ```shell pip install sinch ``` #### Set up your Python application 1. Create a new file named `search-number.py` and paste the provided code found on this page into the file. search-number.py from sinch import SinchClient sinch_client = SinchClient( key_id="YOUR_key_id", key_secret="YOUR_key_secret", project_id="YOUR_project_id" ) available_numbers_response = sinch_client.numbers.available.list( region_code="YOUR_region_code", number_type="YOUR_number_type" ) print(available_numbers_response) Before initializing a client using this SDK, you'll need three pieces of information: - Your Project ID - An access key ID - An access key Secret These values can be found on the [**Access Keys**](https://dashboard.sinch.com/settings/access-keys) page of the Sinch Build Dashboard. You can also [create new access key IDs and Secrets](https://community.sinch.com/t5/Conversation-API/How-to-get-your-access-key-for-Conversation-API/ta-p/8120), if required. Note If you have trouble accessing the above link, ensure that you have gained access to the [Conversation API](https://dashboard.sinch.com/convapi/overview) by accepting the corresponding terms and conditions. 1. Assign your values to the following required parameters: | Parameter | Your value | | --- | --- | | `YOUR_region_code` | The two letter abbreviation of the country for which you'd like a number. Must be in the [ISO 3166-1 alpha-2.](https://en.wikipedia.org/wiki/ISO_3166-2) format. For example, `US`. | | `YOUR_number_type` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. | 1. Save the file. ### Run the code to search for numbers using the Python SDK Now you can run the code to search for an available number. Open a command prompt or terminal to the location where your Python file is saved and run the following command: ```shell python search-number.py ``` These steps should return a JSON list of numbers available to rent. ### Rent and configure a virtual number using the Python SDK Now that you have searched for an available number, now you can rent and configure it for use with SMS and Voice. 1. Create a new file named `rent-config-number.py` and paste the provided code found on this page into the file. rent-config-number.py from sinch import SinchClient sinch_client = SinchClient( key_id="YOUR_key_id", key_secret="YOUR_key_secret", project_id="YOUR_project_id" ) activate_number_response = sinch_client.numbers.available.activate( phone_number="YOUR_phone_number", sms_configuration={ "servicePlanId": "YOUR_service_plan_id", "campaignId": "YOUR_10DLC_campaign_id" }, voice_configuration={ "appId": "YOUR_app_id" } ) print(activate_number_response) This code initializes the Sinch Client and then purchases and configures the number you specify. Before initializing a client using this SDK, you'll need three pieces of information: - Your Project ID - An access key ID - An access key Secret These values can be found on the [**Access Keys**](https://dashboard.sinch.com/settings/access-keys) page of the Sinch Build Dashboard. You can also [create new access key IDs and Secrets](https://community.sinch.com/t5/Conversation-API/How-to-get-your-access-key-for-Conversation-API/ta-p/8120), if required. Note If you have trouble accessing the above link, ensure that you have gained access to the [Conversation API](https://dashboard.sinch.com/convapi/overview) by accepting the corresponding terms and conditions. 1. Assign your values to the following parameters: | Parameter | Your value | | --- | --- | | `YOUR_servicePlanId` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest)This is only required for SMS configuration. | | `YOUR_10DLC_campaignId` | Your 10DLC campaign ID for this campaign. Found on the [TCR](https://community.sinch.com/t5/10DLC/What-is-The-Campaign-Registry-TCR/ta-p/7024) platform. Remove this parameter entirely for non-10DLC numbers. | | `YOUR_appId` | The Voice app ID or voice API `key`. Found in the [Sinch Build Dashboard.](https://dashboard.sinch.com/voice/apps) | | `YOUR_phone_number` | The virtual phone number that you previously searched for and would like to rent. | 1. Save the file. #### Run the code to rent a number using the Python SDK Now you can run 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: ```shell python rent-config-number.py ``` You should get a response similar to this one: ```python ActivateNumberResponse( phone_number='+12067034732', region_code='US', type='LOCAL', capability=['SMS', 'VOICE'] ) ``` PHP ### Search for virtual number using PHP Use this guide to setup your PHP application for use with the Numbers API and search for an available Sinch virtual number. Note: Before you can get started, you need the following already set up: - [PHP](https://www.php.net/manual/en/install.php) 7.3.0 or later and a familiarity with how to create a new file. - cURL 7.61.0 or later installed #### Set up your PHP file 1. Create a file (example: `search-number.php`) and add the supplied sample code. search-number.php "US", "type" => "LOCAL" ); $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_HTTPHEADER => [ "Authorization: Basic " . base64_encode(":") ], CURLOPT_URL => "https://numbers.api.sinch.com/v1/projects/" . projectId . "/availableNumbers?" . http_build_query($query), CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "GET", ]); $response = curl_exec($curl); $error = curl_error($curl); curl_close($curl); if ($error) { echo "cURL Error #:" . $error; } else { echo $response; } ?> 1. Assign your values to the following parameters: | Parameter | Your value | | --- | --- | | `YOUR_username` | your `client_id` or `key_id` found in the [Sinch Build Dashboard](https://dashboard.sinch.com/settings/access-keys). | | `YOUR_password` | Your `client_secret` or `key_secret`. This is generated upon new key creation. Generate a new key if you have lost your secret key. | | `YOUR_ProjectID` | The project ID found in the [Sinch Build Dashboard](https://dashboard.sinch.com/settings/access-keys). | | `regionCode` | The two letter abbreviation of the country for which you'd like a number. [ISO 3166–1 alpha–2. ](https://en.wikipedia.org/wiki/ISO_3166-2) | | `type` | The type of number you would like to rent. Available options are: `MOBILE`, `LOCAL`, or `TOLL_FREE`. Note that 10DLC numbers should be set to `LOCAL`. | 1. Save the file. #### Run the code to search for numbers using PHP Execute the code to search for an available number: ```bash php search-number.php ``` These steps should return a JSON list of numbers available to rent. ### Rent and configure your virtual number using PHP Now that you have searched for an available number, you can rent and configure it for SMS and Voice. 1. Create a file (example: `rent-config-number.php`) and add the supplied sample code. rent-config-number.php array( "servicePlanId" => "YOUR_servicePlanId", "scheduledProvisioning" => array( "status" => "WAITING", "errorCodes" => array( "INTERNAL_ERROR" ) ), "campaignId" => "YOUR_campaignId" ), "voiceConfiguration" => array( "appId" => "YOUR_appId", "scheduledProvisioning" => array( "status" => "WAITING" ) ) ); curl_setopt_array($curl, [ CURLOPT_HTTPHEADER => [ "Content-Type: application/json" ], CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_URL => "https://numbers.api.sinch.com/v1/projects/" . projectId . "/availableNumbers/" . phoneNumber . ":rent", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", ]); $response = curl_exec($curl); $error = curl_error($curl); curl_close($curl); if ($error) { echo "cURL Error #:" . $error; } else { echo $response; } ?> 1. Assign your values to the following parameters: | Parameter | Your value | | --- | --- | | `YOUR_username` | your `client_id` or `key_id` found in the [Sinch Build Dashboard](https://dashboard.sinch.com/settings/access-keys). | | `YOUR_password` | Your `client_secret` or `key_secret`. This is generated upon new key creation in the Sinch Build Dashboard. Generate a new key if you have lost your secret key. | | `YOUR_servicePlanId` | Your [SMS service plan ID](https://dashboard.sinch.com/sms/api/rest)This is only required for SMS configuration. | | `YOUR_10DLC_campaignId` | Your 10DLC campaign ID for this campaign. Found on the [TCR](https://community.sinch.com/t5/10DLC/What-is-The-Campaign-Registry-TCR/ta-p/7024) platform. Remove this parameter entirely for non-10DLC numbers. | | `YOUR_appId` | The Voice app ID or voice API `key`. Found in the [Sinch Build Dashboard.](https://dashboard.sinch.com/voice/apps) This is only required for Voice configuration. | | `YOUR_ProjectID` | The project ID found in the [Sinch Build Dashboard](https://dashboard.sinch.com/settings/access-keys). | | `YOUR_PhoneNumber` | The virtual phone number that you previously searched for and would like to rent. | 1. Save the file. #### Run the code to buy a number using PHP Execute the following code: ```bash php rent-config-number.php ``` You should get a response similar to this one: ```json { "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" } } ``` ## Additional resources Want the whole OpenAPI specification? View it [here](/docs/numbers/api-reference/numbers). If at any point you need more help, you can find support documentation and answers to frequently asked questions in the [Sinch Support Community](https://community.sinch.com/t5/Virtual-Numbers/tkb-p/VirtualNumbers).