Get Started with .NET Core

    Sign up for a Plivo account

    When you sign up with Plivo, we give you a free trial account and free credits to experiment with and learn about our services. You can add a number to your account to start testing the full range of our voice and SMS service features.

    Follow these steps to get a free trial account:

    1. Sign up with your work email address.
    2. Check your inbox for an activation email message from Plivo. Click on the link in the message to activate your account.
    3. Enter your mobile number to complete the phone verification step.

    Sign up with your work email address

    Sign up with email addres

    If you have any issues creating a Plivo account, please contact our support team for assistance.

    To get started, try sending an SMS message either by using our API and XML documents, or via PHLO, our visual design tool, which allows you to create message flows using an intuitive canvas and deploy them with few clicks.

    Send Your First Outbound SMS/MMS

    To send your first outbound sms, you can create and deploy a PHLO in just a few clicks using the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, only pay for sms you send/receive. Building with PHLO is free.

    Prerequisites

    1. Create a Plivo account (if you don’t have one already): Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Set Up Your Dotnet Dev Environment: To set up your dev environment in .NET(C#), please refer to the instructions available in the Set Up Your Dotnet Core Dev Environment section.
    3. Buy a Plivo Number(optional): You must have an SMS-enabled Plivo phone number if you wish to receive incoming sms. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Create the PHLO

    Create a PHLO to send your first outbound SMS/MMS by following these instructions:

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    • From the list of components on the left-hand side, drag and drop the Send Message component onto the canvas. This will add an Send Message node onto the canvas.
    • Connect the Start node with the Send Message node, using the API Request trigger state.
    • Configure the Send Message node with the source/from number using the From field. Enter the destination number you wish to send message in the To field.
    • If you’d like to send MMS then Configure the Media URLs node with the Media files you’d like to send using the Media URLs field.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready. You can trigger the PHLO and test it out. For more information, refer to the following section.

    Set Up Your Dotnet Core Dev Environment

    Install Plivo Dotnet Package

    • Create a project directory, run the following command:

      $ mkdir mydotnetcoreapp
      
    • Change the directory to our project directory in the command line:

      $ cd mydotnetcoreapp
      
    • Install the SDK using Dotnet CLI. You can follow the below commands to install Plivo SDK using Dotnet CLI

      $ dotnet new console
      $ dotnet add package Plivo --version 4.9.0
      

    Trigger the PHLO

    Once you have created and set up your Dotnet Core dev environment, go to your Plivo Console and copy the PHLO_ID. Integrate a PHLO into your application workflow by making an API request to trigger the PHLO with the required payload.

    With Static Payload

    You can choose to either configure the mandatory params required for a PHLO while creating the PHLO itself or, you can pass the params as payload while triggering the PHLO from your app.

    Note: When you configure the params while creating the PHLO, the values are hardcoded and act as static payload.

    With Static Payload

    Code

    Now, open the file in the CS project called Program.cs and paste the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    using System;
    using Plivo;
    
    namespace test_PHLO
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var phloClient = new PhloApi("auth_id", "auth_token"); 
                var phloID = "phlo_id"; // replace the placeholder phlo_id with your PHLO_ID from Plivo Console
                var phlo = phloClient.Phlo.Get(phloID);   
                Console.WriteLine(phlo.Run());
            }
        }
    }
    

    With Dynamic Payload

    To use dynamic values for the parameters, follow the below steps.

    • Select the StartNode - under API request, fill the Payload key as From & To and keep the values empty, then click validate & save.

    With Dynamic Payload

    • Use the liquid templating params while creating the PHLO and pass the values while triggering the PHLO.

    With Dynamic Payload

    Code

    Now, open the file in the CS project called Program.cs and paste the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    using System;
    using System.Collections.Generic;
    using Plivo;
    
    namespace test_PHLO
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var phloClient = new PhloApi("auth_id", "auth_token");
                var phloID = "phlo_id"; // replace the placeholder phlo_id with your PHLO_ID from Plivo Console
                var phlo = phloClient.Phlo.Get(phloID); 
                var data = new Dictionary<string, object>
                {
                    { "From", "+14157778888" },
                    { "To", "+14157778889" }
    
                };  
                Console.WriteLine(phlo.Run(data));
            }
        }
    }
    

    Get your Auth_ID and Auth_token from your dashboard AUTHID

    You can find the PHLO_ID on the PHLO Listing page. PHLO Listing

    Test and Validate

    Save the file and use the below command to run your code.

    $ dotnet run
    
    Note: If you are using a Plivo Trial account for this example, you can only send sms to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Receive Your First Inbound SMS/MMS

    Create and deploy a PHLO to receive your first inbound SMS and forward to your web server with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, only pay for calls you make/receive. Building with PHLO is free.

    Prerequisites

    1. Create a Plivo account (if you don’t have one already): Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo number: You must have a sms-enabled Plivo phone number to receive incoming calls. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
    3. PHLO Application: When you receive a call on a Plivo sms-enabled number, you can control the call flow by associating a PHLO application to that Plivo phone number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the sms.

    Create the PHLO

    You can create a PHLO by referring to the below instructions to receive your first incoming call:

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the Incoming Message trigger state.
    • From the list of components on the left-hand side, drag and drop the HTTP Request component onto the canvas. This will add a HTTP Request node onto the canvas.
    • Connect the Start node with the HTTP Request node, using the Incoming Message trigger state.
    • Configure the HTTP Request node to send the details to your Web Server.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.

    Assign the PHLO to a Plivo Number for Incoming SMS/MMS

    Once you have created and configured your PHLO, assign your PHLO to a Plivo number.

    To assign a PHLO to a number:

    1. Log in to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy a phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now send a message to your Plivo phone number to receive an incoming sms and see how the inbound sms is handled using PHLO.

    For more information about creating a PHLO app, see the PHLO User Guide.
    For information on components and their variables, see the PHLO Components Library.

    Forward an Incoming SMS/MMS

    To forward an incoming sms, you can create and deploy a PHLO with a few clicks on the PHLO canvas.

    Prerequisites

    1. Create a Plivo account (if you don’t have one already): Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo number: You must have a sms-enabled Plivo phone number to receive incoming sms. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
    3. PHLO Application: When you receive a sms on a Plivo sms-enabled number, you can control the flow by associating a PHLO application to that Plivo phone number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the sms.

    Create the PHLO

    You can create a PHLO by referring to the below instructions to forward an incoming SMS/MMS:

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the Incoming Message trigger state.
    • From the list of components on the left-hand side, drag and drop the Send Message component onto the canvas. This will add a Send Message node onto the canvas.
    • Connect the Start node with the Send Message node, using the Incoming Message trigger state.
    • Configure the Send Message node with the phone number to which you want to forward the Message. For example, in this case, “+14157778889”.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.

    Assign the PHLO to a Plivo Number to Forward Incoming SMS

    Once you have created and configured your PHLO, assign your PHLO to a Plivo number.

    To assign a PHLO to a number:

    1. Log in to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy a phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now send a sms to your Plivo phone number and see how the inbound sms is forwarded to the phone number specified in the PHLO.

    For more information about creating a PHLO app, see the PHLO User Guide.
    For information on components and their variables, see the PHLO Components Library.

    Install .NET Core and the Plivo .NET Server SDK

    You must set up and install Dotnet Core(.NET Core 1.0 or higher) and Plivo’s Dotnet SDK to send your first SMS. Here’s how.

    Install Dotnet Core

    Operating SystemInstructions
    macOS & LinuxTo see if you already have Dotnet Core installed, run the command dotnet --version in the terminal. If you do not have it installed, you can install it from here.
    WindowsTo install Dotnet Core on Windows follow the instructions listed here.

    Install Plivo Dotnet Package

    • Create a project directory, run the following command:

      $ mkdir mydotnetcoreapp
      
    • Change the directory to our project directory in the command line:

      $ cd mydotnetcoreapp
      
    • Install the SDK using Dotnet CLI. You can follow the below commands to install Plivo SDK using Dotnet CLI

      $ dotnet new console
      $ dotnet add package Plivo --version 4.9.0
      

    Send Outbound SMS

    Prerequisites

    This section will guide you through how to use Plivo APIs to make voice calls from your application. First, let’s make sure you meet these prerequisites before we dive into the code.

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

      Find Your Auth Credentials on Plivo Console

    • Plivo Phone Number(Optional): Purchase numbers from the Numbers section of your Plivo Console and use the same as the caller ID for the outbound call. This number will also help you receive incoming calls as You must have an SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    Send your first Outbound SMS/MMS

    This section will guide you through how to use Plivo APIs to Send SMS from your application. First, let’s make sure you meet these prerequisites before we dive into the code.

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

    Find Your Auth Credentials on Plivo Console

    • Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Buy a New Plivo Number

    Now, open the file in the CS project called Program.cs and paste the following code.

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    using System;
    using System.Collections.Generic;
    using Plivo;
    
    namespace testplivo
    {
        internal class Program
        {
            public static void Main(string[] args)
            {
                var api = new PlivoApi("<auth_id>","<auth_token>");
                var response = api.Message.Create(
                    src: "+14151234567",
                    dst: new List<String> { "+14157654321" },
                    text: "Hello, from Dotnet-core!"
                    );
                Console.WriteLine(response);
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    using System;
    using System.Collections.Generic;
    using Plivo;
    
    namespace testplivo
    {
        internal class Program
        {
            public static void Main(string[] args)
            {
                var api = new PlivoApi("<auth_id>","<auth_token>");
                var response = api.Message.Create(
                    src: "+14151234567",
                    dst: new List<String> { "+14157654321" },
                    text: "Hello, MMS from Dotnet-core!",
                    type:"mms",
                    media_urls: new string[]{"https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif"},
                    media_ids: new String[]{"801c2056-33ab-499c-80ef-58b574a462a2"}
                    );
                Console.WriteLine(response);
            }
        }
    }
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console.
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • You can use Environment.SetEnvironmentVariable Method to store environment variables and fetch them using Environment.GetEnvironmentVariable Method while initializing the client.
    • Replace the placeholder +14151234567 with the Phone number which you have purchased and +14157654321 with the phone number you will be sending messages to.
    • Both +14151234567 and +14157654321 should be in E.164 format

    Test and Validate

    Save the file and use the below command to run it.

    dotnet run
    
    Note: If you are using a Plivo Trial account for this example, you can only send SMS to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Set up a .NET Core app for Incoming Messages & Callbacks

    In this section, we’ll walk you through how to set up a .Net Framework app in under five minutes and start handling incoming messages & callbacks.

    Plivo supports receiving SMS text messages in several countries (see complete SMS API coverage). When an SMS is sent to a Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL.

    Set up a .NET core app

    • Create a project directory, run the following command:

      $ mkdir receivesmsapp
      
    • Change the directory to our project directory in the command line:

      $ cd receivesmsapp
      
    • Initialize MVC architecture using the below command:

      $ dotnet new mvc --no-https
      
    • Install the Plivo .Net package

      $ dotnet add package Plivo --version 4.9.0
      

    Create a Controller to Receive SMS/MMS

    Navigate to Controllers directory in “receivesmsapp” directory and create a Controller called ReceiveSmsController.cs paste the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    using System;
    using Microsoft.AspNetCore.Mvc;
    
    namespace ReceiceSms.Controllers
    {
        public class ReceiveSmsController : Controller
        {
            // GET: /<controller>/
            public String Index()
            {
                String from_number = Request.Form["From"];
                String to_number = Request.Form["To"]; 
                String text = Request.Form["Text"];
                Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}", from_number, to_number, text);
    
                return "Message received";
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    using System;
    using Microsoft.AspNetCore.Mvc;
    
    namespace ReceiceSms.Controllers
    {
        public class ReceiveSmsController : Controller
        {
            // GET: /<controller>/
            public String Index()
            {
                String from_number = Request.Form["From"]; 
                String to_number = Request.Form["To"];
                String text = Request.Form["Text"];
                String media_url = Request.Form["Media0"];
                Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}, Media:{3}", from_number, to_number, text, media_url);
    
                return "Message received";
            }
        }
    }
    

    Before starting the app, you have to update Properties/launchSettings.json by setting the applicationUrl as

        "applicationUrl": "http://localhost:5000/"
    

    Test and Validate

    Use the below command to start the local server:

      dotnet run
    

    And you should see your basic server app in action on http://localhost:5000/receivesms/

    Exposing your local server to the internet

    To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:

    ./ngrok http <port_on_which_your_local_server_is_running>
    

    This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

    Sample ngrok CLI

    In this case, the ngrok url will be something like https://3b3e783f.ngrok.io/receivesms/.

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application. You can also use Plivo’s Application API.
    2. Give your application a name. Let’s call it Receive SMS. Enter your server URL (e.g. http://example.com/receive_sms/) in the Message URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create to save your application.

    Create Application

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for this app.
    2. Select Receive SMS (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Receive SMS App

    Test and validate

    Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL with the parameters listed in the Messages Documentation.

    Reply to an incoming SMS/MMS

    When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.

    Note: In order to reply to a US or Canadian phone number, the src (source) phone number must be a Plivo US or Canadian phone number that is SMS-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting SMS in the features option to buy SMS enabled numbers.

    Create a Controller called ReplytoInboundController.cs in “Controllers Directory” and paste the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    using System;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace ReceiceSms.Controllers
    {
        public class ReplytoInboundController : Controller
        {
            // GET: /<controller>/
            public IActionResult Index()
            {
                String from_number = Request.Form["From"];
                String to_number = Request.Form["To"];
                String text = Request.Form["Text"];
    			  Plivo.XML.Response resp = new Plivo.XML.Response();
    			  resp.AddMessage("Hi, message from Plivo.",
    							new Dictionary<string, string>()
    			{
    				{"src", to_number},
    				{"dst", from_number},
    				{"type", "sms"},
    				{"callbackUrl", "http://foo.com/sms_status/"},
    				{"callbackMethod", "POST"}
    			});
    			var output = resp.ToString();
    			Console.WriteLine(output);
    			return this.Content(output, "text/xml");
    		}
        }
    }
    
    Note: Create an application in the Plivo console with your Server URL in the message_URL field and assign the app to a Plivo number.

    Test and validate

    Use the below command to start the local server:

      dotnet run
    

    Send an SMS to your Plivo number using a regular mobile phone and an automatic response will be sent from your Plivo number to your mobile phone. Also, Plivo will send a request to your Message URL with the parameters listed in the Messages Documentation.