Testing DataMotion APIs with Insomnia REST Client
Tags: tutorial, api, testing, productivity
Published: 2023-06-19 18:47:36

> [Video demonstration](https://www.youtube.com/watch?v=K3a5sf7aYqw&t=1s)

## Introduction
APIs, or Application Programming Interfaces, play a critical role in enabling seamless interaction and data exchange between software applications. As the backbone of many web-based services, APIs have become increasingly important, delivering efficiency and functionality across platforms.

Testing these APIs is a crucial step to ensure smooth operation and early detection of any potential issues. In this guide, we'll walk you through how to test and validate DataMotion's Secure Message Center API using the Insomnia REST Client, a user-friendly tool suitable for developers of all experience levels.

DataMotion aims to empower users to seamlessly integrate secure data exchange directly into their workflows. By leveraging DataMotion's robust APIs and no-code solutions, you can work within your enterprise applications while delivering a secure and streamlined experience to your end-users.

## Prerequisites
Before we begin, ensure you have the following:

- An active [DataMotion](https://datamotion.com/portal/project/DataMotion/dashboard?utm_campaign=dev-blog&utm_source=dev-to) account. You'll need this to access the Secure Message Center API.
- The Insomnia REST Client installed on your system. If you haven't installed it yet, download it from the [official website](https://insomnia.rest/).

## Acquiring the Access Token
The first step in API testing is authentication. To make API calls to DataMotion's v3 APIs, an Access Token is required. This token can be obtained by making a POST request to the Token endpoint.

Detailed information about the Token API endpoint, along with other Secure Message Center APIs, can be found in [DataMotion's Developer Center](https://developers.datamotion.com/?utm_campaign=dev-blog&utm_source=dev-to). For the sake of this tutorial, here are the steps to obtain the Access Token:

- In your Insomnia workspace, create a new POST request and name it `Get Access Token`.
- The URL for the request is: `https://api.datamotion.com/SMC/Messaging/v3/token` for DataMotion customers, and `https://api.datamotion.com/SMC-Sandbox/Messaging/v3/token` for trial members.
- In the Body section of the request, select `JSON` and input the following:
```json
{
	"grant_type": "client_credentials",
  	"client_id": "your-client-id",
  	"client_secret": "your-client-secret"
}
```
Remember to replace "your-client-id" and "your-client-secret" with your actual credentials.

- After confirming that your details are correct, send the request. A successful request will return a 200 status code and your Access Token in the response.

Here's what a successful token request looks like:

![Token API Call](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afutc0ify4pdi3b9ztgq.jpg)

## Testing the 'Get Message Summaries' API Call
With your Access Token in hand, you're now ready to start testing the various Secure Message Center API endpoints. We'll start with the 'Get Message Summaries' API call, which provides a summary of all your messages.

Following the same process as above, we can find details for the 'Get Message Summaries' API call in [DataMotion's Developer Center](https://developers.datamotion.com/?utm_campaign=dev-blog&utm_source=dev-to). Here's how to set up the request in Insomnia:

- Create a new GET request and name it `Get Message Summaries`.
- The URL for this request is: `https://api.datamotion.com/SMC/Messaging/v3/content/messages/?folderId=1&pageSize=10&pageNumber=1&sortDirection=DESC&metadata=true` for DataMotion customers, and `https://api.datamotion.com/SMC-Sandbox/Messaging/v3/content/messages/?folderId=1&pageSize=10&pageNumber=1&sortDirection=DESC&metadata=true` for trial members.
- In the Headers section, add your Access Token for authorization and set the "Content-Type" to "application/json".
```json
{
	"Authorization": "Bearer your-access-token",
	"Content-Type": "application/json"
}
```
Ensure you replace "your-access-token" with the actual Access Token.

- After confirming your details are correct, send the request. If successful, you'll receive a summary of your messages in the response.

Here's how a successful API call setup looks:

![Get Messages API Call](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30piwgdddfat2vf6v7ad.jpg)

## Handling API Errors
During testing, you may encounter errors, which typically appear as 400, 401, 403, or 500 response codes. These codes often signal issues with your request parameters or Access Token. In case of an error, carefully review your request setup and retry the request.

Here's an example of what an error response may look like:
```json
{
	"error": "InvalidRequest",
	"errorMessage": "Invalid request.",
	"data": null
}
```
Remember, encountering errors is part of the learning process. Troubleshooting these issues will further hone your proficiency in API testing!

## Conclusion
Congrats - you've successfully begun testing APIs using DataMotion's Secure Message Center APIs and the Insomnia REST Client. This is just the start—continue applying these principles to other APIs and advancing your skills.

For more examples, check out my previous blog post on [Composing and Sending a Secure Message with DataMotion APIs](https://dev.to/janellephalon/compose-and-send-a-secure-message-in-10-minutes-or-less-with-datamotion-apis-3akm). Further, [DataMotion's Developer Center](https://developers.datamotion.com/?utm_campaign=dev-blog&utm_source=dev-to) is a comprehensive resource on all their APIs.

Keep learning, keep testing, and happy coding!