Nexmo

SMS - Nexmo

Nexmo

Give your customers the possibility to respond to your SMS

Convenient two-way business SMS anywhere in the world

  • Communication

API reference on SwaggerHub

Introduction

SMS can be sent or received using the SMS API. Go ahead and experiment with the API. In your My Apps dashboard apply for production. We will follow up on your request.

Conceptual model

Conceptual model

Definitions

WebHook

A WebHook is a HTTP POST callback implemented by you, that can be called by another system when an event is triggered on that system to notify you. You will need to make the address of your webhook known on the other system to make it work.

SMS

SMS stands for Short Message Service and is the most widely used type of text messaging. With an SMS, you can send a message of up to 160 characters to another device.

API workflow

API workflow

Prerequirements

For receiving an SMS, this API depends on the Nexmo - Phone Numbers API, which you can use to create a virtual number.

Features and constraints

Features

Send SMS

You can send SMS using mobile number, content and sender parameters in the request body of the SMS API.

Receive SMS

SMS using this API is easy. In order to do so, you'll need to create a webhook endpoint. Then you should register it using the SMS API. After you have registered your webhook endpoint/callback URL you can send an SMS using the mobile number, content, and sender parameters in the request body and selecting the yes option in the ‘inbound’ header of the SMS API. Inbound messages sent by SMS recipient will be delivered to this registered webhook.

Constraints

Getting started

Authentication

To authenticate you'll need to request an access token. Use your API Store app's credentials (Consumer Key and Consumer Secret) to make an authentication request. The authorization service returns a JSON message that contains the access_token field.

Use one of the following 3 options:

cURL

Execute below cURL command to receive an access token. Replace APP_CONSUMER_KEY and APP_CONSUMER_SECRET with your app's credentials.

curl -X POST \
 'https://api-prd.kpn.com/oauth/client_credential/accesstoken?grant_type=client_credentials' \
 -H 'content-type: application/x-www-form-urlencoded' \
 -d 'client_id=APP_CONSUMER_KEY&client_secret=APP_CONSUMER_SECRET'

If you are using cURL for Windows, please use the command below instead.

curl -X POST "https://api-prd.kpn.com/oauth/client_credential/accesstoken?grant_type=client_credentials" -H "content-type: application/x-www-form-urlencoded" -d "client_id=APP_CONSUMER_KEY&client_secret=APP_CONSUMER_SECRET"

The authentication service returns a JSON message that contains the access token field.

{
    "refresh_token_expires_in": "0",
    "api_product_list": "[xxxxxxx]",
    "api_product_list_json": [
        " xxxxxxx"
    ],
    "organization_name": "kpn",
    "developer_email": "demo123@kpn.com",
    "token_type": "Bearer",
    "issued_at": "1521039195424",
    "client_id": "APP_CONSUMER_KEY",
    "access_token": "haf2SDl07E9N7RluNQ4kJ1TkGgso",
    "application_name": "6e38edxxxxxxxxxxxxxxxx4065d79c",
    "scope": "",
    "expires_in": "3599",
    "refresh_count": "0",
    "status": "approved"
}

SwaggerHub

  1. Click on the Authorize button on the top right.
  2. In the form, fill in client_id and client_secret, using your app's credentials.
  3. Click Authorize.

Postman

When using Postman, you will have to import the Swagger file into a Postman collection as follows:

  1. Open the API reference on SwaggerHub.
  2. On the top right, click Export, click Download API and click 'YAML Unresolved'.
  3. In Postman from the menu click File and click Import... Choose the YAML file you downloaded in the previous step. A new collection will be added.
  4. Select Get Access Token from the collection.
  5. Make sure the right environment is selected, corresponding to the API.
  6. Edit the environment variables client_id and client_secret, using your app's credentials.
  7. Check the response code and message.
  8. Press the Send button to get an access token.

Note: Request variables are no longer linked to an environment, but to the collection.

How to...

Send SMS

SwaggerHub:

  1. Select POST /send.
  2. Click Try it out.
  3. Edit the parameters by filling out from, to, text and other fields at will.
  4. Click Execute.
  5. Check the response code and message.

Postman:

  1. Select (POST) Send SMS.
  2. Click the Body section of the request and provide values for the following keys: from, to and text. Other advanced keys can be filled at will.
  3. Click Send.
  4. Check the response code and message.

Result example:

{
  "message-count": "1",
  "messages": [
    {
      "to": "316*****",
      "message-id": "13000000*****",
      "status": "0",
      "remaining-balance": "*****",
      "message-price": "*****",
      "network": "20408"
    }
  ]
}

Receiving an SMS

Handling inbound SMS with with the API is easy. You just need to create a webhook endpoint and configure your number or account to point to the endpoint. If you are a test user then you can try inbound messaging using below using 'Register Webhook endpoint'. Otherwise, you should use the Phone Numbers API to search, buy and manage your virtual number. In order to use the Phone Numbers API you should take this App containing Phone Numbers API to production.

Register Webhook endpoint

With a test application, you can try inbound messaging by registering your webhook against your own phone number you use for testing, without buying a virtual number. This is not applicable if you apply for production usage.

SwaggerHub:

  1. Select POST /register.
  2. Click Try it out.
  3. Edit the parameters by filling out to and callback_url.
  4. Click Execute.
  5. Check the response code and message.

Postman:

  1. Select (POST) Register callback URL to webhook for incoming messages.
  2. Click the Bodysection of the request and provide values for the following keys: to and callback_url.
  3. Click Send.
  4. Check the response code and message.

Result example:

{
  "message": "Your callback url is registered."
}

If you applied for production then you should manage your numbers using the Phone Numbers API. Under 'Update a number' you can register your webhook against your virtual number using the moHttpUrl parameter

curl -X POST \
  'https://api-prd.kpn.com/communication/nexmo/phone-numbers/number/update' \
  -H 'authorization: Bearer access_token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'country=NL&msisdn=447700900000&moHttpUrl=https://example.com:webhooks/inbound-sms'

SwaggerHub:

  1. Select POST /number/update.
  2. Click Try it out.
  3. Edit the parameters by filling out country and msisdn. Also edit the other parameters, as mentioned in above table, to your needs.
  4. Click Execute.
  5. Check the response code and message.

Postman:

  1. Select (POST) Update a number.
  2. Click the Bodysection of the request and provide values for country and msisdn. Also edit the other parameters in the body, as mentioned in above table, to your needs.
  3. Click Send.
  4. Check the response code and message.

Result example:

{
  "error-code":"200",
  "error-code-label":"success"
}

Send and Receive SMS

Send an SMS. If you are using a test application then VirtualNumber will be your own number that you registered with your webhook. When you reply to the number then you will see the response in your webhook.

SwaggerHub:

  1. Select POST /send.
  2. Click Try it out.
  3. Edit the parameters by filling out from (use your own number or the VirtualNumber), to, text and other fields at will.
  4. Click Execute.
  5. Check the response code and message.

Postman:

  1. Select (POST) Send SMS.
  2. Click the Body section of the request and provide values for the following keys: from (use your own number or the VirtualNumber), to and text. Other advanced keys can be filled at will.
  3. Click Send.
  4. Check the response code and message.

Result example:

{
  "message-count": "1",
  "messages": [
    {
      "to": "316*****",
      "message-id": "13000000*****",
      "status": "0",
      "remaining-balance": "*****",
      "message-price": "*****",
      "network": "20408"
    }
  ]
}

Return codes

Code   Description
200   Success
201   Created
202   Accepted
302   Found. Link in location header
400   Bad request
401   Unauthorized
403   Forbidden
404   Not found
405   Method not allowed
412   Precondition failed
429   Too many requests
500   Internal server error
502   Bad gateway
503   Service unavailable