TUTORIAL

Send SMS with Ruby - 3 Simple Steps to Get Started with 46elks

If you know how to program in Ruby 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 Ruby.

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 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 it’s time to login 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 on the blurred text under API password to see your own password.

Every time you do something with 46elks, like sending an SMS, you have to provide your username and password via Basic HTTP Authentication. When you are using Rubys standard Net::HTTP library you need to set Basic Authentication username and password for every request (see the code example below).

3. Send an SMS

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

There are many ways to implement this in Ruby. This is an example of how it can be done using only standard libraries:

require 'net/http'

username = 'YOUR USERNAME' # Specify your API username
password = 'YOUR PASSWORD' # Specify your API password

uri = URI('https://api.46elks.com/a1/SMS')
req = Net::HTTP::Post.new(uri)
req.basic_auth username, password 
req.set_form_data(
  :from => 'Ruby', # The sender of the SMS, up to 11 characters
  :to => 'YOUR PHONE NUMBER', # The number that will receive the text message
  :message => 'It is easy to send SMS with 46elks' # The content of the text message
)

res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|    
  http.request req 
end

if (res.code != 200)
  puts 'Failure: ' + res.body
else 
  puts 'Success: ' + res.body
end

The example is tested in Ruby 2.1.2

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.

Use the Ruby example by creating a send.rb file, copy/paste the example code above to the file, add your username, password and phone number to the code. Then execute it by using the command ruby send.rb and your phone will buzz. Congratulations, you’ve just sent an SMS from Ruby.

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 Ruby code examples at GitHub.

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