Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Mock API Generation with Twilio's OpenAPI Spec


(information)

Info

The Twilio OpenAPI Spec is currently in PUBLIC BETA . The spec is expected to be accurate, but is currently in active development and is unsupported . If you've identified a mismatch between Twilio's API behavior and the specification, please open an issue(link takes you to an external page).

Thanks to the rollout of Twilio's OpenAPI specification(link takes you to an external page), it is now possible to mock Twilio's API locally and in your CI environments. Using a mock can save you time and money during development and testing.


What is a mock API server?

what-is-a-mock-api-server page anchor

A mock API server allows you to emulate a real API (such as Twilio) locally, in your test environments, and/or even publicly. This mock server can receive requests, and will respond to them with static or dynamic data that simulates what the real API would return.


Why use a mock API server?

why-use-a-mock-api-server page anchor

Developing and/or testing against a mock API server has a litany of benefits, including but not limited to:

  • Testing against Twilio costs money per request, while testing the same requests against a mock are effectively free.
  • A mock API server is typically stateless, so you can repeatedly test the same scenario and expect consistent results.
  • A locally running mock API server will have substantially faster response times than a live API service, which can add up to time savings both in local dev and especially for lengthy test suites. This will also allow you to develop while not connected to the internet, or in a situation where workplace policies may prevent your machine from connecting to Twilio.
  • A mock API server can simulate the behavior of endpoints which are still under development or in a private beta before they've been released, unblocking your own development process

Additionally, because the mock server in this example will be generated using Twilio's OpenAPI Specification, each endpoint will validate requests and return data in the same format as the live Twilio API.

If you're interested in taking advantage of these benefits, let's proceed and learn how to set up the mock.


Mocking Twilio with the OpenAPI Specification and Prism

mocking-twilio-with-the-openapi-specification-and-prism page anchor

Prerequisites

prerequisites page anchor

In order to generate a mock server from Twilio's OpenAPI specification(link takes you to an external page), you will use the open source tool, Prism(link takes you to an external page).

What is Prism? According to the Prism documentation:

Prism is an open-source HTTP mock server that can mimic your API's behavior as if you already built it. Mock HTTP servers are generated from your OpenAPI v2/v3 (formerly known as Swagger) documents.

Install Prism using your preferred package manager as shown below:


_10
npm install -g @stoplight/prism-cli
_10
_10
# OR
_10
_10
yarn global add @stoplight/prism-cli

Running the mock API server with Prism

running-the-mock-api-server-with-prism page anchor

Now that Prism is installed, the next step is to feed a specification file to Prism so that it can generate the desired mock API server. Run the following command:


_10
prism mock https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json

(information)

Info

Note that we're referencing the JSON spec files in our examples. It's also perfectly valid to use the YAML files instead, but for the sake of this article we will only be focusing on JSON.

After a short time you will see a trace similar to below. Once you see the green start line the server is running and listening for requests on the displayed port on localhost. Congratulations on starting your (maybe first) Prism mock server!


_10
$ prism mock https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json
_10
_10
› [CLI] … awaiting Starting Prism…
_10
› [CLI] ℹ info GET http://127.0.0.1:4010/2010-04-01/Accounts.json?FriendlyName=omnis&Status=suspended&PageSize=801
_10
› [CLI] ℹ info POST http://127.0.0.1:4010/2010-04-01/Accounts.json
_10
› [CLI] ℹ info GET http://127.0.0.1:4010/2010-04-01/Accounts/ACbC8dB063DcfAe78a6D0cfEb09e0c7c7a/Addresses.json?CustomerName=doloribus&FriendlyName=porro&IsoCountry=necessitatibus&PageSize=325
_10
_10
› [CLI] ℹ info GET http://127.0.0.1:4010/2010-04-01/Accounts/ACF5ce8F97eb3E0270a856d8539e61DAE5.json
_10
› [CLI] ℹ info POST http://127.0.0.1:4010/2010-04-01/Accounts/AC61FCB0720E02eCAf7fEeCbafEa602aE5.json
_10
› [CLI] ▶ start Prism is listening on http://127.0.0.1:4010

Testing the mock API's validation layer

testing-the-mock-apis-validation-layer page anchor

To validate that this is working as expected, let's make some requests to the mock using curl in various forms to see some features of the mock in action.

First, leave the Prism mock server running in the terminal, and open a new tab or window depending on what's allowed by your terminal. In the new terminal instance, let's start with the example of querying the Twilio API for a list of messages sent by your account. If we try the following command and forget to include credentials, we will get the following error:


_10
curl -s -D "/dev/stderr" http://127.0.0.1:4010/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json | json_pp

Which will result in:


_21
HTTP/1.1 401 Unauthorized
_21
Access-Control-Allow-Origin: *
_21
Access-Control-Allow-Headers: *
_21
Access-Control-Allow-Credentials: true
_21
Access-Control-Expose-Headers: *
_21
content-type: application/problem+json
_21
WWW-Authenticate: Basic realm="*"
_21
Content-Length: 327
_21
Date: Wed, 31 Mar 2021 18:41:14 GMT
_21
Connection: keep-alive
_21
Keep-Alive: timeout=5
_21
_21
{
_21
"headers" : {
_21
"WWW-Authenticate" : "Basic realm=\"*\""
_21
},
_21
"title" : "Invalid security scheme used",
_21
"detail" : "Your request does not fullfil the security requirements and no HTTP unauthorized response was found in the spec, so Prism is generating this error for you.",
_21
"status" : 401,
_21
"type" : "https://stoplight.io/prism/errors#UNAUTHORIZED"
_21
}

Also, if you take a peek at the Prism terminal instance, you will see some validation errors as below:


_10
› [HTTP SERVER] get /2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json ℹ info Request received
_10
› [NEGOTIATOR] ℹ info Request contains an accept header: */*
_10
› [VALIDATOR] ⚠ warning Request did not pass the validation rules
_10
› [NEGOTIATOR] ⬤ debug Unable to find a response definition
_10
› [NEGOTIATOR] ⬤ debug Unable to find a 'default' response definition
_10
› [HTTP SERVER] get /2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json ✖ error Request terminated with error: https://stoplight.io/prism/errors#UNAUTHORIZED: Invalid security scheme used

Notice here that the mock API server is actually validating the content of our request as the real Twilio API would, and is throwing a 401 error since credentials were not included in the request headers. Let's fix that and try again once we've set some environment variables for Twilio credentials


_10
export TWILIO_ACCOUNT_SID=AC…
_10
export TWILIO_AUTH_TOKEN=...
_10
_10
curl -s -D "/dev/stderr" http://127.0.0.1:4010/2010-04-01/Accounts/MockSID/Messages.json -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN | json_pp

And the result will be:


_37
HTTP/1.1 422 Unprocessable Entity
_37
Access-Control-Allow-Origin: *
_37
Access-Control-Allow-Headers: *
_37
Access-Control-Allow-Credentials: true
_37
Access-Control-Expose-Headers: *
_37
content-type: application/problem+json
_37
Content-Length: 508
_37
Date: Wed, 31 Mar 2021 18:41:43 GMT
_37
Connection: keep-alive
_37
Keep-Alive: timeout=5
_37
_37
{
_37
"title" : "Invalid request",
_37
"detail" : "Your request is not valid and no HTTP validation response was found in the spec, so Prism is generating this error for you.",
_37
"status" : 422,
_37
"validation" : [
_37
{
_37
"message" : "should NOT be shorter than 34 characters",
_37
"severity" : "Error",
_37
"code" : "minLength",
_37
"location" : [
_37
"path",
_37
"accountsid"
_37
]
_37
},
_37
{
_37
"code" : "pattern",
_37
"location" : [
_37
"path",
_37
"accountsid"
_37
],
_37
"message" : "should match pattern \"^AC[0-9a-fA-F]{32}$\"",
_37
"severity" : "Error"
_37
}
_37
],
_37
"type" : "https://stoplight.io/prism/errors#UNPROCESSABLE_ENTITY"
_37
}

Note that this time the mock API server successfully validated the Account SID (in this case, MockSID) and found that it failed validation due to being the wrong length and for including invalid characters. The mock is doing a great job of emulating the real Twilio API so far!

Creating a successful request against the mock API server

creating-a-successful-request-against-the-mock-api-server page anchor

If we cut the shenanigans and give the mock API server valid input and see what happens. Run the following command:


_10
curl -s -D "/dev/stderr" http://127.0.0.1:4010/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN | json_pp

and the output will be:


_45
HTTP/1.1 200 OK
_45
Access-Control-Allow-Origin: *
_45
Access-Control-Allow-Headers: *
_45
Access-Control-Allow-Credentials: true
_45
Access-Control-Expose-Headers: *
_45
Content-type: application/json
_45
Content-Length: 551
_45
Date: Wed, 31 Mar 2021 18:43:18 GMT
_45
Connection: keep-alive
_45
Keep-Alive: timeout=5
_45
_45
{
_45
"uri" : "http://example.com",
_45
"next_page_uri" : "http://example.com",
_45
"start" : 0,
_45
"first_page_uri" : "http://example.com",
_45
"messages" : [
_45
{
_45
"account_sid" : null,
_45
"num_segments" : null,
_45
"uri" : null,
_45
"subresource_uris" : null,
_45
"to" : null,
_45
"direction" : "inbound",
_45
"body" : null,
_45
"status" : "queued",
_45
"num_media" : null,
_45
"messaging_service_sid" : null,
_45
"api_version" : null,
_45
"error_code" : null,
_45
"from" : null,
_45
"error_message" : null,
_45
"date_updated" : null,
_45
"date_sent" : null,
_45
"date_created" : null,
_45
"sid" : null,
_45
"price_unit" : null,
_45
"price" : null
_45
}
_45
],
_45
"previous_page_uri" : "http://example.com",
_45
"page_size" : 0,
_45
"page" : 0,
_45
"end" : 0
_45
}

Success! This caused the mock server to return a 200 status as well as a JSON representation of a messages sent record. The Prism terminal instance should also reflect this success as shown:


_10
› [HTTP SERVER] get /2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json ℹ info Request received
_10
› [NEGOTIATOR] ℹ info Request contains an accept header: */*
_10
› [VALIDATOR] ✔ success The request passed the validation rules. Looking for the best response
_10
› [NEGOTIATOR] ✔ success Found a compatible content for */*
_10
› [NEGOTIATOR] ✔ success Responding with the requested status code 200

Now that we've validated the mock API server is running and acting as expected, the next step is to make it so that our applications can communicate with the mock instead of sending requests to Twilio during development and testing.


Connecting applications to the Prism mock server

connecting-applications-to-the-prism-mock-server page anchor

In order for you to leverage the Prism mock server during development of your application code, your Twilio client will need to be configured to redirect requests to the mock instead of Twilio itself. We'll demonstrate how to accomplish this in Node.js below, but note that this is supported by all of the Twilio SDKs.

As mentioned in the Twilio Node documentation on custom HTTP clients(link takes you to an external page), it's possible to extend the behavior of the Twilio client by creating and passing in a custom implementation of the httpClient property. This is a perfect point in the codebase to add some code that will intercept requests and redirect them to the mock.

Starting from within a new empty directory, create a file for housing your custom Request Client. In this case let's call it PrismClient.js.

Next, add the following example code:

Custom Twilio Client for Prism Mock Server

custom-twilio-client-for-prism-mock-server page anchor

_13
class PrismClient {
_13
constructor(prismUrl, requestClient) {
_13
this.prismUrl = prismUrl;
_13
this.requestClient = requestClient;
_13
}
_13
_13
request(opts) {
_13
opts.uri = opts.uri.replace(/^https\:\/\/.*?\.twilio\.com/, this.prismUrl);
_13
return this.requestClient.request(opts);
_13
}
_13
}
_13
_13
exports.PrismClient = PrismClient;

This PrismClient has a few key points:

  • It is constructed using a url for the locally running Prism instance as well as the existing Twilio RequestClient. Relying on the existing client means we can focus only on our custom behavior and not worry about fully re-creating the client.
  • The request method uses a regular expression to change the url of any request intended for Twilio to instead hit the same relative path, but on the Prism server instead.

To use this new client, let's create an index.js in the same directory that sends a new SMS Message. Additionally, let's make sure the necessary dependencies are also available, such as the Twilio Node SDK:


_11
yarn init -y
_11
# Or if you prefer npm
_11
npm init -y
_11
_11
# Once initialized, install dependencies!
_11
yarn add twilio dotenv
_11
# Or with npm
_11
npm i twilio dotenv
_11
_11
# Create a new js file as well as a safe file for credentials and other sensitive variables to live in
_11
touch index.js .env

Next, paste the following example code into index.js

Using the Prism Mock Client

using-the-prism-mock-client page anchor

_22
require('dotenv').config();
_22
_22
const twilio = require('twilio');
_22
const { PrismClient } = require('./PrismClient');
_22
_22
const { RequestClient } = twilio;
_22
_22
const apiKey = process.env.TWILIO_API_KEY;
_22
const apiSecret = process.env.TWILIO_API_SECRET;
_22
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_22
const to = process.env.TO_NUMBER;
_22
const from = process.env.FROM_NUMBER;
_22
_22
const client = twilio(apiKey, apiSecret, {
_22
accountSid,
_22
httpClient: new PrismClient('http://127.0.0.1:4010', new RequestClient()),
_22
});
_22
_22
client.messages
_22
.create({ to, from, body: 'Ahoy!' })
_22
.then((message) => console.log(`Message ${message.sid} sent!`))
_22
.catch((error) => console.error(error));

(information)

Info

Note that we're using best practices here, and creating the Twilio client using an API Key and Secret instead of directly using and potentially exposing your account's auth token. If you'd like to learn more about API Keys and how to create one, please refer to the API Keys documentation!

There are a few important things happening here of note:

  • This snippet is importing twilio , the custom PrismClient that we just created earlier, and destructuring out twilio s internal RequestClient .
  • After declaring all variables being pulled in from the environment, we're creating a new Twilio client using account credentials, and more importantly providing a custom httpClient as an option. The first argument is the url and port of the locally running Prism server, and the second is the Twilio RequestClient that was imported earlier.
  • Finally, we're using this client to create and send an 'Ahoy!' SMS to the provided number. Once the message is queued, a log will be printed to the terminal along with the SID of that message (or we'll safely catch and log just in case there's some kind of error)

Similarly, add the following variables to your .env file, being sure to use your own valid Account SID, API Key, Key Secret, and Twilio/Personal phone numbers.


_10
# .env
_10
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
_10
TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
_10
TWILIO_API_SECRET=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
_10
TO_NUMBER=+12345678901
_10
FROM_NUMBER=+12345678901

Execute the example code by running node index.js.

Once execution completes, you'll see the following in your terminal:


_10
$ node example.js
_10
Message null sent!

Don't be alarmed by the null, this is simply a result of Prism successfully intercepting the request and returning null for the message sid!

If you switch over to the terminal that is running Prism, you will see logs indicating a successful request/response cycle from Prism that returned a 201:


_10
› [HTTP SERVER] post /2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json ℹ info Request received
_10
› [NEGOTIATOR] ℹ info Request contains an accept header: application/json
_10
› [VALIDATOR] ✔ success The request passed the validation rules. Looking for the best response
_10
› [NEGOTIATOR] ✔ success Found a compatible content for application/json
_10
› [NEGOTIATOR] ✔ success Responding with the requested status code 201

This is an example of being able to run your code locally and verify that your integration with Twilio is correct. Note that the request to create and send an SMS never reached Twilio (you shouldn't have received a text!), and instead was quickly and safely redirected to the mock instead.

(error)

Danger

If your code uses the .list or .each methods that are available (such as client.messages.list()), running your code against the mock will cause it to throw an error. This is because the mocked next_page_uri value will be http://example.com, which will cause the request to 404 against Prism.

(warning)

Warning

If your application makes calls to other subdomains than api.twilio.com, this mocking solution will not intercept calls to those other subdomains yet. It's possible to duplicate this process for other files one at a time (for example, mocking for accounts.twilio.com but not api.twilio.com), however we are still developing a solution to allow mocking for multiple subdomains at once.


Making the mock server more portable with Docker

making-the-mock-server-more-portable-with-docker page anchor

If you want to share this mock with other developers on your team without the need for them to install Stoplight, or make it easily reproducible in another environment such as your CI pipeline, running it as a Docker container is highly advised. If you do not have Docker already installed, you can download and install it from here(link takes you to an external page).

Creating and running Stoplight using Docker

creating-and-running-stoplight-using-docker page anchor

To run the previous mock API server using the Stoplight Docker image instead of the Stoplight CLI, run the following example command. Be sure to end any previously running Prism processes, otherwise the port will already be taken and the Docker container will fail to start:


_10
docker run --init -d -p 4010:4010 stoplight/prism:4 mock -h 0.0.0.0 https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json

Once the Docker container is built (it may take some time depending on connection speeds), it will begin running and accepting requests on localhost:4010 just as before with the CLI.

You can verify that your container is running using the following command and looking for the stoplight/prism:4 image as below:


_10
docker ps

You may also leverage the Docker Dashboard(link takes you to an external page) for a GUI-based way of viewing the running container and its logs.

Now that you have a successfully running, Dockerized version of the Prism mock it's time to verify it is working as expected. Run the node example again via node example.js, and once again you should see:


_10
$ node example.js
_10
Message null sent!

If you consult the Docker Dashboard for your container, you should see logs indicating that a message request was received and that a 201 was returned, just as we saw for the non-Dockerized version of Prism.

When you want to stop your container, first list out the running Docker images, then identify either the Container ID or Name of the running Prism container.


_10
$ docker ps
_10
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
_10
c1f2b80c13ae stoplight/prism:4 "node dist/index.js …" 3 seconds ago Up 2 seconds 0.0.0.0:4010->4010/tcp, :::4010->4010/tcp youthful_banzai

In this example, you could use either of these commands to stop the container:


_10
# Using the ID
_10
docker stop c1f2b80c13ae
_10
# Using the Name
_10
docker stop youthful_banzai

Simplify running the Dockerfile with Docker Compose

simplify-running-the-dockerfile-with-docker-compose page anchor

If you'd like to further abstract away the process of building and running the Stoplight Docker image, you have the option of configuring and using Docker Compose to simplify the process and reduce friction for other team members.

First, create a new docker-compose.yml file:


_10
touch docker-compose.yml

and paste in the following contents:


_10
# docker-compose.yml
_10
version: '3'
_10
services:
_10
prism:
_10
image: stoplight/prism:4
_10
ports:
_10
- '4010:4010'
_10
command: mock -h 0.0.0.0 https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json

You can now run the Dockerized Prism container with an even shorter command:


_10
docker-compose up -d
_10
# compare to:
_10
# docker run --init -d -p 4010:4010 stoplight/prism:4 mock -h 0.0.0.0 https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json

The Docker container will now be running and accessible on localhost:4010 just as before, and you can run the node example (node example.js) once more to verify.

Sharing your Docker setup

sharing-your-docker-setup page anchor

Now that you have Docker set up in this way, you could commit the docker-compose.yml file to version control alongside the project(s) that leverage these files for testing within your team or organization.


Rate this page: