Interactive Guide to the Best Practices for API Parameters

Fri Mar 04 2022

3 min read

Parameters carry the information required by the API to process requests. Both API designers and consumers need to know the dos and don'ts of these request parameters. This guide will highlight some best practices that can help in this regard.

In simple terms, API request parameters are options that can be passed with the endpoint to influence the response. They are like search filters; they single out the data you want to receive from the API.

These parameters are of different types, which we have already explained in this guide. Today, we will focus on the best practices while using them.

Best Practices

The four main types of request parameters are:

  • Header Parameters
  • Path Parameters
  • Query String Parameters
  • Request Body Parameters

Let's discuss some recommendations for each type.

Header Parameters

If a parameter you want to add stays the same on all endpoints, it is better to include it as a header parameter.

Use Path Parameters to Identify Collections

Path parameters are included in the URL path of the endpoint and represented by curly braces like this:

js
/blog/posts/{id}

It is best to use path parameters when you want to identify a specific object of a resource type. They cover required parameters identified by unique identifiers such as id in the above example.

Use Query Parameters to Filter Collections

While path parameters are unique identifiers that identify an object in a resource, query parameters are used to filter or sort the objects in a resource. For example, the following query parameter will filter out all the JavaScript posts:

js
/blog/posts?tag=javascript

So, query parameters are more suited for filtering, sorting, searching, or pagination.

When to Avoid Query Parameters

It goes without saying that sensitive information must not be passed using query parameters because they are visible in the URL. If there are too many query parameters in a resource URL, the URL may become too long and hard to debug. In that case, you may consider changing the endpoint design or sending a Request Body in a POST request instead.

Query parameters are primarily used in GET requests to query data, and they are not meant to send data to the API. If you want to send data, use the request body in a POST request.

Use Request Body for Sending Data

The parameter types above fetch data from the API, but when you want to send some data to the server through the API, use the request body.

json
{
"guests": 3,
"city": "san francisco",
"time": 1433524597
}

Try the following quick trivia about API Request Parameters and see if you can get the answers right.

What type of parameters should be used for getting the recent posts on a `/posts` endpoint ?
Path Parameters
Query Parameters
Body Parameters
Headers

Wrap Up

That's pretty much it. We hope some of these best practices will help you effectively use the request parameters.