How to: Build Mock API Calls

Overview

We typically refer to mock API (application programming interface) calls as test API APIs (application programming interfaces) are a set of protocols and definitions developers use to build and integrate application software. APIs act as the connective tissue between products and services. calls. In this case, "mock" refers to temporarily setting up an internal API call in the place of an external one. This comes into play when the external service you want to integrate isn't quite ready. But, you don't want your external service holding up your build. So, you can set up a mock API call while you configure in your staging and UAT (User Acceptance Testing) environments. Once the external service is ready, you'll switch over to using the actual external API call. In this lesson, we'll talk about mock API calls and how to use them at Unqork.

What Is a Mock API Call?

Some general rules and concepts surround mock API calls. For instance, a mock call doesn't have to work exactly the same way as your external call. It just needs the same inputs and possible outputs as the external call. The key is to get the same response as the external call without having to duplicate it. When configured, it should return values with the same format as the external call. So, it's important to know input parameters and responses for the external call. External services typically provide this information on their website.

Setting Up a Mock API Call

Now that you know how to use a mock API call, let's talk about how to set one up. Here we'll specifically talk about setting up mock API calls at Unqork.

1. First, you'll configure 2 modules to trigger your API calls:
a. Mock API module: This module runs your mock (internal) API call. The key to creating a successful mock API call is to mimic the behavior of the actual external service. The most important part of this process is to understand the service's documentation. Once you understand the documentation, you can begin building your mock API call. The module you'll configure to run your mock API call is the Mock API module. You'll configure this to run in all environment levels but production. You never want to test or run mock API calls in your production environment level. If the external service's API response is simple, you can build data using Calculator or Data Workflow components. For more complex API responses, a transform is best for generating the response.
b. Service module: This module runs your service (external) API call. You'll configure this module to run your actual external API call. You only want to run the external service when the application is complete. At that point, you won't need your mock API call. That means you'll set up this module up only when the external API endpoint is ready. How you configure this API call is dependent on the external service you're using. So, it's important to know how the request and response bodies will look. Reading and understanding the service's available documentation will help with the configuration. It's important to note that you must add the external service to the for your external API call to work.

To learn more about transforms, search Transforms Sidebar Option in our In-Product Help. And to learn more about added services, search Services Administration in our In-Product Help.

2. Next you'll select and set up your Control Type. Your Control Type is how you'll determine the current environment of your end-user. This part "controls" which API call to trigger based on the environment. Your options include configuring a control module, global variables, or getEnv.
  • Control Module: This module gives you a central place to control which calls trigger. This is the best option if you have multiple external calls to trigger. You'll add a Radio Buttons component for your mock and external API call. If you have multiple services, assign Property IDs that reflect the service. This will help you keep everything organized. You'll use the Radio Buttons to select the API call you want to run.

  • Global Variables: You can also create the same functionality using Unqork's Global Variables Administration. This feature lets you create environment variables so you can reference them later. You can also easily promote global variables between different environment levels. You'll enter vars in your logic component input to reference your global variable. For instance, let's say you have a global variable called mockOrService. Now, let's say you want to reference the variable in a Calculator component. To do this, you'll enter vars.mockOrService in the Calculator's input.

  • getEnv: The last option is to use a Calculator component to find the current environment. You can do this using the getEnv function. This is the easiest and quickest option if you want the environment to drive the decision. All you have to do is configure a Calculator and enter an input of getEnv. In the DevTools Console, you'll see an environment_group output. This output tells you the environment level. For instance, if you do a getEnv in your staging environment, the output is environment_group: "staging".

To learn more about global variables, search Global Variables Administration in our In-Product Help.

3. And lastly, you'll configure a Decision module. The Decision module triggers your mock or external call based on your control's response. You'll use a Plug-In component to retrieve this information. This Plug-In configuration depends on which control type you chose.

Below is a diagram to help you visualize the process: