Webhooks are an extension of an API, but instead of your code requesting data from Nexmo, Nexmo sends data to you. The data arrives in a web request to your application. A webhook may be the result of an earlier API call (this type of webhook is also called a "callback"), such as an asynchronous request to the Number Insight API. Webhooks are also used to notify your application of events such as an incoming call or message.
Since the Nexmo servers need to be able to send data to your application via webhooks, you need to set up a webserver to receive the incoming HTTP requests. You also need to specify the URL of each webhook on your webserver so that data can be sent to each one.
With webhooks, it's important that the URL to send the webhooks to is configured. When there is data available, Nexmo sends the webhook to your application as an HTTP request. Your application should respond with an HTTP success code to indicate that it successfully received the data.
The process looks something like this:
Webhooks provide a convenient mechanism for Nexmo to send information to your application for events such as an incoming call or message, or a change in call status. They can also be used to send follow-up information such as a delivery receipt which may become available some time after the request it relates to.
Information resulting from requests to the SMS API, Voice API, Number Insight API, US Short Codes API, and Nexmo virtual numbers are sent in an HTTP request to your webhook endpoint on an HTTP server. To configure your webhook endpoint, please visit the Nexmo dashboard
Nexmo sends and retrieves the following information using webhooks:
|API Name||Webhooks usage|
|SMS API||Sends the delivery status of your message and receives inbound SMS|
|Voice API||Retrieves the Nexmo Call Control Objects you use to control the call from one webhook endpoint, and sends information about the call status to another. View the Webhook Reference for more detail.|
|Number Insight Advanced Async API||Receives complete information about a phone number|
|US Short Codes API||Sends the delivery status of your message and receives inbound SMS|
|Client SDK/Conversation API||Real-time Communication (RTC) events are sent to the RTC event webhook|
|Message and Dispatch APIs||Supports both inbound message and message status webhooks|
Webhooks are used to deliver incoming messages and delivery receipts.
To set up the webhook used for incoming messages, go to the Your Numbers section of the Nexmo Dashboard. Click 'edit' for the virtual number and set the Callback URL.
You can also use the nexmo-cli to set the incoming messages endpoint for an individual number.
See the Delivery Receipts guide in the SMS documentation.
The Number Insight Advanced API allows you to have the results of a number lookup sent synchronously or asynchronously.
callback argument to a webhook URL to receive the lookup asynchronously.
See Number Insight Advanced Async for more details.
For Voice API requests, webhooks can be set at an application level, when creating a call, or in the actions in an NCCO.
Numbers you have purchased that are connected to Nexmo applications will use the
answer_url to retrieve an NCCO, and the
event_url to send call status information to you.
You can set a status webhook for each number you purchase. This will be used to send events to you regarding each number.
When making a new outbound call, you need to set the
answer_url in the call to a URL containing an NCCO. Nexmo's servers will retrieve the NCCO from this endpoint and follow its instructions in handling the outbound call.
The payload for the
||The number being called|
||The number making the call|
||The UUID of the conversation|
||The UUID of the leg|
Inside an NCCO, the following action types take a webhook URL for use when that action is executed:
- record.eventUrl - set the webhook endpoint that receives information about the recording for a Call or Conversation
- conversation.eventUrl - set the URL to the webhook endpoint Nexmo calls asynchronously when a conversation changes state for this conversation action
- connect.eventUrl - set the URL to the webhook endpoint Nexmo calls asynchronously when a conversation changes state for this connect action
- input.eventUrl - set the URL to the webhook endpoint Nexmo sends the digits pressed by the callee
- stream.streamUrl - set an array of URLs pointing to the webhook endpoints hosting the audio file to stream to the Call or Conversation
You set the URL of the RTC event webhook when you create the application using your language of choice.
More information on creating an application with RTC capabilities can also be found in the Application API documentation.
There are two webhooks supported by the Messages and Dispatch APIs, the Message Status webhook and the Inbound Message webhook. Message status is inbound on the Message Status webhook and the Message itself is received on the Inbound Message webhook. Setting these webhooks is described in detail in the topic Configuring webhooks for Messages and Dispatch APIs.
To interact with Nexmo webhooks:
- Create a Nexmo account.
- Write scripts to handle the information sent or requested by Nexmo. Your server must respond with a success status code (any status code between 200 OK and 205 Reset Content) to inbound messages from Nexmo.
- Publish your scripts by deploying to a server (for local development, try Ngrok).
- Configure a webhook endpoint in the API you would like to use.
- Take an action (such as sending an SMS) that will trigger that webhook.
Information about your request is then sent to your webhook endpoint.
Start simple - Publish the simplest possible script that you can think of to respond when the webhook is received and perhaps print some debug information. This makes sure that the URL is what you think it is, and that you can see the output or logs of the application.
Code defensively - Inspect that data values exist and contain what you expected before you go ahead and use them. Depending on your setup, you could be open to receiving unexpected data so always bear this in mind.
Look at examples - Nexmo provides examples implemented with several technology stacks in an attempt to support as many developers as possible. For example code using webhooks see the following:
You can also check the code snippets section of the documentation for the API you are using.
Webhooks are an unusual situation for developers; often you will work on local platforms while you're getting the details worked out, but when using webhooks your webhook URLs must be publicly accessible in order for Nexmo or another service to access them. One way to work around this problem is to use a free tool called Ngrok. Ngrok creates a secure tunnel to your locally-running application from the outside world. To get started with Ngrok, follow these steps:
- Download and install Ngrok using the instructions for your platform.
- Start your webserver running locally, and check which port it uses (for example, if you usually use http://localhost:3000 to access your application, then the port is 3000).
- Create an Ngrok tunnel to this port with a command such as
ngrok http 3000.
- This command will show the URL of the tunnel, which will look similar to
https://abcdef1.ngrok.io. Copy this URL.
- Go ahead and configure your Nexmo webhooks to point to this URL, and you are up and running.
Beware that each time you start up the Ngrok tunnel, you're likely to get a new URL so don't forget to update the webhooks configuration accordingly! Paid Ngrok accounts can reserve tunnel names and choose which one to use when they start up, so you can avoid having to reconfigure your webhooks.
Cool bonus feature of Ngrok: in addition to the tunnel URL displayed in the command output, you will also see a URL for the "Web Interface". The web interface offers an excellent way of examining the details of all the requests received by the tunnel and the responses returned and is, therefore, a very useful debugging tool. You can also use the "Replay" button to repeat a request rather than having to send yourself lots of text messages or continuously reproduce whatever event your webhook code is responding to. These features make testing your application's response to webhooks much more convenient.
If you restrict inbound traffic (including delivery receipts), you need to whitelist the following IP addresses in your firewall. Inbound traffic from Nexmo might come from any of the following: