TUTORIAL

Send SMS with #C - 3 Simple Steps to Get Started with 46elks SMS API

If you know how to program in C# you can send text message with 46elks. Just follow these three simple steps to get started and in a few minutes you will receive an SMS that you have sent from C#.

Swedish: Är du i Sverige så rekommenderar vi att du läser den svenska versionen av denna guide.

1. Create a user

To send an SMS with 46elks you first have to create an account. Go to the registration page and fill in your details. You will then receive an automatic phone call to verify your new account.

To start using 46elks to send a text messages you will need to purchase some credits. The minimum purchase is 10 EUR which is more than enough to get started.

2. Username and password

Once you have an account you will be automatically loged in to the 46elks dashboard. This is where you can see all your important account information - like the username and password you need to send an SMS. The username is called API username and the password API password.

Tip: Click/hover on the blured text under API password to see your own API-password.

Every time you do something with 46elks, like sending an SMS, you have to provide your API-username and API-password via Basic HTTP Authentication.

3. Send an SMS

Finally, it’s time to write some C# code. Your code needs to be able to do the following:

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace elktest {
  class Program {
    public static string user = "<API-username>"; // Specify your API username
    public static string pwd = "<API-password>"; // Specify your API password
    private static HttpClient client;
    static void Main(string[] args)
    {
      client = new HttpClient();
      client.BaseAddress = new Uri("https://api.46elks.com");

      client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue(
          "Basic",
          Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
              string.Format("{0}:{1}", user, pwd)
            )
          )
        );

      var content = new FormUrlEncodedContent(new[] {

        // The sender of the SMS, 3 to 11 characters
        new KeyValuePair < string, string > ("from", "ElkCo"),

        // The number that will receive the text message
        new KeyValuePair < string, string > ("to", "+46700000000"),

        // The content of the text message
        new KeyValuePair < string, string > (
          "message","It's cold outside, bring a sweater!"),
      });

      HttpResponseMessage response = client
        .PostAsync("/a1/SMS", content).Result;

      // If not 200 OK then SMS is sent and it is possible to handle response.
      if(response.StatusCode == HttpStatusCode.OK){
        string result = response.Content.ReadAsStringAsync().Result;

        // Decode the data from the API:
        SMS sms = JsonConvert.DeserializeObject<SMS>(result);
        Console.WriteLine(sms.id);
        Console.WriteLine(sms.parts);
        Console.WriteLine(sms.cost);
      }
      // Else there is an error and the response will be a string with an error description.
      else{
        string result = "Error: " + response.Content.ReadAsStringAsync().Result;
        Console.WriteLine(result); // Typicaly throw an error here.
      }
    }
  }
  // Class for response object from the 46elks SMS API.
  public class SMS {
    public string id { get; set; }
    public string from { get; set; }
    public string to { get; set; }
    public string message { get; set; }
    public string status { get; set; }
    public string created { get; set; }
    public string direction { get; set; }
    public int parts { get; set; }
    public string owner { get; set; }
    public int cost { get; set; }
  }
}

Project file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>
</Project>

The example is tested in .NET Core 1.1.0

Tip: The phone number to send to should be formatted with country code and without spaces, dashes or parentheses. For example +468123123 and not 08-123123 or +46 (8) 123 123.

If you are new to .NET Core, here are some additional pointers for you:

  1. Download and Install .NET Core.
  2. Open your terminal and run the following commands:
dotnet new console -o send_sms_with_46elks_and_csharp
cd send_sms_with_46elks_and_csharp
vim Program.cs # Use your favorite code editor to replace the content of the file with the example code.
dotnet add package Newtonsoft.Json
dotnet restore
dotnet run

Next step

You’ve just sent a text message with a text sender (i.e. not from a phone number). By doing this you can use SMS to remind customers of reservations, send confirmations of new orders, etc.

To send an SMS is the easiest way to get started with 46elks, but you can do so much more. For example you can receive SMS, automatically respond to an incoming SMS or handle voice calls. For this and more there are a load of C# examples at GitHub.

If you have any questions then get in touch, we love talking to our users!