Rent and configure your virtual number using .NET Core

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:

Rent and configure your virtual number

Rent and configure a virtual number

using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;
using Newtonsoft.Json.Linq;

public class Program
{
  public static async Task Main(string[] args)
  {
    using (var client = new HttpClient())
    {
      var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"YOUR_username:YOUR_password"));
      client.DefaultRequestHeaders.Add("Authorization", "Basic "+ base64String);
      JObject json = JObject.Parse(@"{
        smsConfiguration: {
          servicePlanId: 'YOUR_servicePlanId',
          scheduledProvisioning: {
            status: 'WAITING',
            errorCodes: ['INTERNAL_ERROR']
          },
          campaignId: 'YOUR_10DLC_campaignId'
        },
        voiceConfiguration: {
          appId: 'YOUR_appId',
          scheduledProvisioning: {status: 'WAITING'}
        }
      }");
      var postData = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
      var ProjectId = "YOUR_ProjectId";
      var PhoneNumber = "YOUR_PhoneNumber";
      var request = await client.PostAsync("https://numbers.api.sinch.com/v1/projects/" + ProjectId + "/availableNumbers/" + PhoneNumber + ":rent", postData);
      var response = await request.Content.ReadAsStringAsync();

      Console.WriteLine(response);
    }
  }
}

Fill in your parameters

  1. Paste the Rent and configure a virtual number code in place of the search for a virtual number code in the Program.cs file.
  2. Replace following parameter values on your Program.cs file:
ParameterYour value
YOUR_usernameyour client_id or key_id found in the Sinch Customer Dashboard.
YOUR_passwordYour 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_servicePlanIdYour SMS service plan IDThis is only required for SMS configuration.
YOUR_10DLC_campaignIdYour 10DLC campaign ID for this campaign. Found on the TCR platform. Remove this parameter entirely for non-10DLC numbers.
YOUR_appIdThe Voice app ID or voice API key. Found in the Customer Dashboard.
YOUR_ProjectIDThe project ID found in the Customer Dashboard.
YOUR_PhoneNumberThe virtual phone number that you previously searched for and would like to rent.
  1. Save the file.

Build and run your project

  1. Before executing your code, you must first compile your application. Execute the following command:
Copy
Copied
dotnet build
  1. Now you can execute the code. Run the following command:
Copy
Copied
dotnet run

Response

You should get a response similar to this one:

Copy
Copied
{
  "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

Was this page helpful?

Rent and configure a virtual number

using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;
using Newtonsoft.Json.Linq;

public class Program
{
  public static async Task Main(string[] args)
  {
    using (var client = new HttpClient())
    {
      var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"YOUR_username:YOUR_password"));
      client.DefaultRequestHeaders.Add("Authorization", "Basic "+ base64String);
      JObject json = JObject.Parse(@"{
        smsConfiguration: {
          servicePlanId: 'YOUR_servicePlanId',
          scheduledProvisioning: {
            status: 'WAITING',
            errorCodes: ['INTERNAL_ERROR']
          },
          campaignId: 'YOUR_10DLC_campaignId'
        },
        voiceConfiguration: {
          appId: 'YOUR_appId',
          scheduledProvisioning: {status: 'WAITING'}
        }
      }");
      var postData = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
      var ProjectId = "YOUR_ProjectId";
      var PhoneNumber = "YOUR_PhoneNumber";
      var request = await client.PostAsync("https://numbers.api.sinch.com/v1/projects/" + ProjectId + "/availableNumbers/" + PhoneNumber + ":rent", postData);
      var response = await request.Content.ReadAsStringAsync();

      Console.WriteLine(response);
    }
  }
}