API Testing Tools and Techniques Every Developer Should Master
4 ways to test your APIs. I realized 2nd option very late.
If you are a backend developer, you would mostly be developing APIs for frontend or other backend applications to consume.
In this blog, I will share different tools and techniques to test APIs.
1. Postman
Postman and its relatives are very popular tools for API testing. It provides a nice interface for visualizing the request body, response, and headers.
Postman also provides support for setting env variables which can be used for storing API keys or secrets.
When to use it
For backend-to-backend testing, which mostly relies on API keys as authorization
For APIs which doesn’t require a lot of prerequisites. If you can call your API using an API key or some auth headers then Postman makes it easy to call
2. Browser console
When a frontend is rendered in the browser, the javascript code uses the browser to call backend APIs. You can see all those requests from the networks tab in the browser. These are all fetch requests. You can then copy the whole request and paste it into the browser console to replay the request.
When to use it
This option is best when the auth session or token is stored in cookies by your frontend.
Some frontend applications use cookies to store sessions or tokens. This makes it easy for the frontend to make backend calls without having to explicitly pass tokens in each request. The browser automatically picks cookies and sends them with requests to the server.
Although you can set cookies in Postman as well, it’s very convenient to test from the browser console since you already have cookies stored in the browser.
Remember: You have to be on the same website domain as the domain of the cookies.
Custom clients
I would suggest this option if you want customization on inputs to APIs.
Postman and browser console are easy to use but they can’t be customized too much. You can’t just run a loop with different inputs.
This approach is using your favorite language to build a custom client of your API. If you built REST API, you can use Http client in chosen language. If you built GRPC API, you can find GRPC client or if you built graphQL API you can use Apollo client to test API.
I use javascript language and node js environment to build my custom client. It’s very easy to run a javascript file using node.
Create a javascript file called weather.js
and copy following code
const https = require('https');
const url = "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m";
https.get(url, (response) => {
let data = '';
// A chunk of data has been received.
response.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received.
response.on('end', () => {
console.log(JSON.parse(data));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
Run file using node
node weather.js
For the ultimate developer — use curl
Curl is a command line tool (cli) which can be used to make network requests. You can call REST, GRPC or GraphQL endpoints.
curl "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m"
When to use it
It’s very difficult to format requests for curl. I would suggest only use it for simple API for a quick testing. If you don’t want to install different applications just for testing an API, curl is your friend.
Thank you for reading till the end
If you liked the blog
- Please consider clapping (👏)
- Follow me for more resources on developer productivity