Build Mock APIs that Return Dynamic Responses with Response Templating

Tom Akehurst
CTO and Co-founder
September 19, 2023

In this article, we show you can use dynamic response templating - a powerful WireMock Cloud feature that allows a mock API to serve different data using a single stub. We’ll demonstrate how to use values from a request in the response in order to create an illusion of serving multiple data items. 

Watch the step by step video tutorial [03:30 minutes] or read the tutorial below:

What is response templating?

When creating a mock API, you can use response templating to provide variable responses based on the incoming request. This allows you to simulate the behavior of real-world APIs more accurately, since production APIs tend to deliver different responses based on various conditions in the incoming request. This allows for more realistic tests and allows you to examine how downstream components will interact with the API in different scenarios.

Let’s look at an example…

To better illustrate how response templating works, let’s imagine that we're developing against the API of a CRM system. In the CRM, each contact has a unique ID; an API call that requests information about a specific contact (such as their full details) would use this ID in the request. 

If we were just using a static return response, the mock API would always return the ‘details’ of the same contact. Essentially, we'd be testing our system with the same data repeatedly, which is unlikely to reflect the reality of how the API works. We could theoretically create a new mock response with a separate path parameter and response for each contact ID, but clearly this would become inefficient very quickly. 

That’s where response templating comes in: you can use a blank parameter and create a dynamic response that will be valid for any contact ID. This means you only need a single stub to handle all requests, regardless of the contact ID specified in the request URL. This makes your mock API more flexible and keeps it easy to maintain.

3 Steps to build the dynamic mock API 

(If you don’t have a WireMock Cloud account, create one for free here)

1. Create a new stub

Start by creating a new stub. For the method, choose GET. Choose path template as the match type, and include {contactId} in the URL form

2. Enable dynamic response templating

Go ahead and enable dynamic response templating:

This feature uses a handlebars templating system for response headers and the response body (more on this below). Set the content type to application/json, which tells the system that you'll be serving a JSON response.

3. Create a response template

Let’s create a basic contact record that will be returned as a response. This will include an ID, and a name. (It's common for API responses to have an ID field that matches the ID in the URL path.)


{
	"id": "{{{request.path.contactId}}}",
	"name": "Tom"
}

What this template is doing is telling the WireMock server to return the value from the response, rather than a fixed value. (We could also ‘templatize’ the name rather than use a fixed value, but are keeping it simple for the purposes of this tutorial)

(Note that we’re using handlebars template syntax. The triple brackets are used to indicate that the content should not be escaped, but repeated as is. Essentially this means that when a request is made, the request is broken out into a model and made available to the templating engine.)

We’re done!

That’s it, our stub is ready to use. When you call this mock API, you will receive a different contact ID in your response, based on the request. Of course, you can take this further and add additional templated elements - but we’ve already created a mock that is far more realistic in the way it simulates the real API, and which can provide an unlimited number of different responses based on a single stub.

Next steps:

/

Latest posts

Have More Questions?