Before you Begin

This topic provides some background information on using the code snippets.

What are Code Snippets?

Code snippets are short pieces of code you can reuse in your own applications.

The code snippets utilize code from the Vonage Node Code Snippets and Vonage Curl Code Snippets repositories.

Please read this information carefully, so you can best use the code snippets.

Prerequisites

  1. Create a Vonage API Account
  2. Install Node JS - required for using the Nexmo Command Line Interface (CLI).
  3. Install the Beta version of the Nexmo CLI
  4. Install or update the Beta version of the Node Server SDK - only required if using Node.
  5. Configure webhooks.
  6. Know how to write a webhook server
  7. Know how to test your webhook server locally

Replaceable variables

Generic replaceable

The following replaceable information depends on the library and specific call:

Key Description
VONAGE_API_KEY Your Vonage API key (see it on your dashboard).
VONAGE_API_SECRET Your Vonage API secret (also available on your dashboard).
VONAGE_APPLICATION_PRIVATE_KEY_PATH Private key path.
VONAGE_APPLICATION_PRIVATE_KEY Private key.
VONAGE_APPLICATION_ID The Vonage Application ID.

Numbers

All phone numbers are in E.164 format.

Key Description
VONAGE_NUMBER Your Vonage Number. E.g. 447700900000
FROM_NUMBER Replace with number you are sending from. E.g. 447700900002
TO_NUMBER Replace with the number you are sending to. E.g. 447700900001

Specific replaceable/variables

Some code snippets have more specialized variables, such as Facebook Page IDs, that will need to be replaced by actual values. Where required, these are specified on a per-code snippet basis.

Webhooks

The main ones you will meet here are:

  • /webhooks/inbound-message - You will receive a callback here when Vonage receives a message.
  • /webhooks/message-status - You will receive a callback here when Vonage receives a message status update.

If you are testing locally using Ngrok you will set your webhook URLs in the Vonage Application object using a format similar to the following examples:

  • https://demo.ngrok.io/webhooks/inbound-message
  • https://demo.ngrok.io/webhooks/message-status

Change demo in the above with whatever Ngrok generates for you, unless you have paid for a reusable URL.

NOTE: Make sure your webhook URLs are configured and your webhooks server is running before testing your application.

Generate a JWT

TIP: You only need to do this step if you are testing with Curl, as the client libraries generate JWTs as needed for you.

Once you have created a Vonage API Application you can use the Application ID and the private key file, private.key, to generate a JWT.

TIP: If you are using the Server SDK for Node (or other languages when supported), the dynamic creation of JWTs is done for you.

If you're using the Nexmo CLI, the command to create the JWT is:

Copy to Clipboard
JWT="$(nexmo jwt:generate private.key exp=$(($(date +%s)+86400)) application_id=NEXMO_APPLICATION_ID)"

This JWT will be valid for one day. After that, you will need to generate a new one.

You can then view the JWT with:

Copy to Clipboard
echo $JWT

Alternatively, you can use our JWT web app to generate a JWT.

TIP: You can test your generated JWT at jwt.io