Get Started with .NET Framework
Sign up for a Plivo Account
You can sign up with Plivo and start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and buy a number by clicking here. Add a number and credits to your account to test the full range of our voice and SMS service features.
Sign up here to get your free Plivo account today.
Follow these steps to sign up for a free trial account:
- Sign up with your work email address
- Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
- Enter your mobile number to complete the phone verification step.
Sign up with your work email address
If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.
Install .NET Framework and the Plivo Nuget package
You must set up and install Dotnet Framework(.NET Framework 4.6 or higher) and Plivo’s Dotnet SDK to make your first call. Here’s how.
Install Dotnet Framework
Operating System | Instructions |
---|---|
OS X & Linux | To see if you already have Dotnet Framework installed, run the command dotnet --version in the terminal. If you do not have it installed, you can install it from here. |
Windows | To install Dotnet Framework on Windows follow the instructions listed here. |
Install Plivo .NET Package using Visual Studio
- Create a new project in Visual Studio
- Choose a Template for the new project
- Install the Plivo Nuget package
Send your first Outbound SMS
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!
- 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.
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, this is test message"
);
Console.WriteLine(response);
}
}
}
- 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 making calls to.
- Both +14151234567 and +14157654321 should be in E.164 format
Before starting the app, you have to update Properties/launchSettings.json by setting the applicationUrl as
"applicationUrl": "http://localhost:5000/"
Save the file and run it.
Set up a .NET Framework 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 9 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 Framework app
- Create a MVC web app:
- Configure the MVC app and provide a project name:
- Install the Plivo Nuget package
Create a Controller to Receive Calls
Navigate to Controllers directory in “ReceiveSms” app and create a Controller named 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
20
21
22
23
using System;
using Microsoft.AspNetCore.Mvc;
namespace ReceiceSms.Controllers
{
public class ReceiveSmsController : Controller
{
// GET: /<controller>/
public String Index()
{
// Sender's phone number
String from_number = Request.Form["From"];
// Receiver's phone number
String to_number = Request.Form["To"];
// The text which was received
String text = Request.Form["Text"];
// Print the message
Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}", from_number, to_number, text);
return "Message received";
}
}
}
Before starting the app, you have to update Properties/launchSettings.json by setting the applicationUrl as
"applicationUrl": "http://localhost:5000/"
Run the project 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.
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.
In this case, the ngrok url will be something like https://3b3e783f.ngrok.io/receivesms/.
Create an Application
- Create an Application by visiting the Application Page and click on
New Application
. You can also use Plivo’s Application API. - Give your application a name. Let’s call it
Receive SMS
. Enter your server URL (e.g.http://example.com/receive_sms/
) in theMessage URL
field and set the method asPOST
. See our Application API docs to learn how to modify your application through our APIs. - Click on
Create
to save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
- Select
Receive SMS
(name of the app) from the Plivo App dropdown list. - Click on
Update
to save.
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 XML Request - Messages Documentation.
Reply to an incoming SMS
To forward an incoming call, you need to use Dial XML.
- You should set up the dial XML document in your server
- URL of the dial XML in your server has to be set up as the answer_url of the app assigned to the Plivo number.
- During an incoming call to your Plivo number, Plivo will send a request to the answer_url expecting a valid XML to process the incoming call.
- As the response would be a dial XML, the call will be forwarded to the number defined in the XML response.
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
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace ReceiceSms.Controllers
{
public class ReplytoInboundController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
Plivo.XML.Response resp = new Plivo.XML.Response();
resp.AddMessage("Hi, message from Plivo.",
new Dictionary<string, string>()
{
{"src", "+12023222222"},
{"dst", "+15671234567" } ,
{"type", "sms"},
{"callbackUrl", "http://foo.com/sms_status/"},
{"callbackMethod", "POST"}
});
var output = resp.ToString();
Console.WriteLine(output);
return this.Content(output, "text/xml");
}
}
}
Test and validate
Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL
requesting for a valid XML response and process the call. Once Plivo receives the dial XML response, the call will be forwarded to the number defined in the XML. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.