Receiving an SMS

Handling inbound SMS with Nexmo is easy. You just need to create a webhook endpoint and configure your number or account to point to the endpoint.

Prerequisites

Implement a webhook endpoint

const app = require('express')()
const bodyParser = require('body-parser')

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app
  .route('/webhooks/inbound-sms')
  .get(handleInboundSms)
  .post(handleInboundSms)

function handleInboundSms(request, response) {
  const params = Object.assign(request.query, request.body)
  console.log(params)
  response.status(204).send()
}

app.listen(3000)

Run your server

Save this file to your machine and run it using the node command:

$ node app.js

Implement a webhook endpoint

port(3000);

get("/webhook/inbound-sms", (req, res) -> {
    logger.info("GET request received!");
    for (String param : req.queryParams()) {
        System.out.printf("%s: %s\n", param, req.queryParams(param));
    }

    res.status(204);
    return "";
});

post("/webhook/inbound-sms", (req, res) -> {
    logger.info("POST request received, with type: " + req.contentType());

    // The body will be form-encoded or a JSON object:
    if (req.contentType().startsWith("application/x-www-form-urlencoded")) {
        for (String param : req.queryParams()) {
            System.out.printf("%s: %s\n", param, req.queryParams(param));
        }
    } else {
        IncomingSmsPayload jsonPayload = IncomingSmsPayload.fromJson(req.bodyAsBytes());
        System.out.println(jsonPayload);
    }

    res.status(204);
    return "";
});

Run your server

Add the following dependencies to your project:

compile 'com.nexmo:client:3.1.0'
compile "com.sparkjava:spark-core:2.6.0"

Then run your class in whichever way you find easiest.

Implement a webhook endpoint

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;

$handler = function (Request $request, Response $response) {
    $params = $request->getParsedBody();

    // Fall back to query parameters if needed
    if (!count($params)){
        $params = $request->getQueryParams();
    }

    error_log(print_r($params, true));

    return $response->withStatus(204);
};

$app->get('/webhooks/inbound-sms', $handler);
$app->post('/webhooks/inbound-sms', $handler);

$app->run();

Run your server

Save this file to your machine and run it using the php command:

$ php -t . -S 127.0.0.1:3000

Implement a webhook endpoint

from flask import Flask, request, jsonify
from pprint import pprint

app = Flask(__name__)

@app.route('/webhooks/inbound-sms', methods=['GET', 'POST'])
def delivery_receipt():
    if request.is_json:
        pprint(request.get_json())
    else:
        data = dict(request.form) or dict(request.args)
        pprint(data)

    return ('', 204)

app.run(port=3000)

Run your server

Save this file to your machine and run it using the python command:

$ python app.py

Implement a webhook endpoint

require 'sinatra'
require 'sinatra/multi_route'
require 'json'

helpers do
  def parsed_body
     json? ? JSON.parse(request.body.read) : {}
  end

  def json?
    request.content_type == 'application/json'
  end
end

route :get, :post, '/webhooks/inbound-sms' do
  puts params.merge(parsed_body)
  status 204
end

set :port, 3000

Run your server

Save this file to your machine and run it using the ruby command:

$ ruby app.rb

You'll need to expose your server to the open internet. During development you can use a tool like Ngrok  to do that.

Configure your SMS endpoint with Nexmo

From Nexmo Dashboard  go to Settings  .

Enter your endpoint in the field labeled Webhook URL for Inbound Message:

Screenshot

Send your Nexmo number an SMS

Now when you send your Nexmo number an SMS you should see it logged in your console.