How to Use Algolia APIs with Postman: A Developer’s Guide – Part 1

You are currently viewing How to Use Algolia APIs with Postman: A Developer’s Guide – Part 1
  • Post category:Search
  • Post last modified:

Algolia provides a powerful APIs that allow developers to interact with search indices, retrieve records, run search queries and manage search related data programmatically. Postman is a convenient tool for exploring and testing Alogolia requests during day-to-day development.

In this blog post, I’ll walk through practical examples of using Algolia API with Postman, including authentication, sending requests, working with indices and retrieving search results. Whether you’re troubleshooting an integration, validating API responses or simply learning how Algolia API works, these examples will help you get started quickly.

Before making API calls, let’s configure Postman with required environment variables and authentication details. This allows us to reuse the same configuration across multiple requests without repeatedly entering values.

To make it easier to follow along with the practical examples in this blog, I recommend creating a free Algolia account. I created my free account using Getting Started Tutorial as mentioned below:

https://www.algolia.com/doc/guides/get-started/quickstart

I’m going to explain the setup that works for my project but it’s entirely up to you how and where you configure your variables.

Environment variable

Set up indexName variable in Environment variable in Postman. I decided to choose it because in my case I have separate index name per environment.

Configure Authentication Headers

Algolia’s REST API examples use the x-algolia-application-id and x-algolia-api-key headers for authentication. I decided to set up authentication headers at Collection level in Before request section as shown below and configured actual values under Variables tab.

I have configured baseURL as https://{{applicationId}}.algolia.net

pm.request.headers.add({
    key: "x-algolia-application-id",
    value: "{{applicationId}}"
});

pm.request.headers.add({
    key: "x-algolia-api-key",
    value: "{{apiKey}}"
});

Let’s now explore API calls. Please note that I’ll use variable names in the queries, assuming you have already created these variables during the initial setup as explained above.

This method search a single index and returns a matching results as specified in the Body

Request: POST {{baseURL}}/1/indexes/{{indexName}}/query
Body:
{
  "params": "query=toy story 5&hitsPerPage=10"
}

This query search for values as specified for facet attribute

Request: POST {{baseURL}}/1/indexes/{{indexName}}/facets/product_type/query
Body:
{
  "params": "hitsPerPage=2&getRankingInfo=1",
  "facetQuery": "tops",
  "maxFacetHits": 10
}

Before you make an API call, make sure particular facet is marked as searchable otherwise you’ll receive this error message:

{
    "message": "Cannot search in `product_type` attribute, you need to add
    `searchable(product_type)` to attributesForFaceting.",
    "status": 400
}

To make facet as searchable, go to the index and select configuration tab. Search facet and then select searchable option as shown below:

This query fetch one record by object Id. In below example, “media-sample-data-1084244” is the object id of record “Toy Story 5” from algolia_movie_sample_dataset index

Request: GET {{baseURL}}/1/indexes/{{indexName}}/media-sample-data-1084244

By default, all attributes are returned in API response but you can use attributesToRetrieve attribute as a query parameter to reduce the response size as mentioned below

Request: GET {{baseURL}}/1/indexes/{{indexName}}/media-sample-data-1084244?
attributesToRetrieve=title,overview

Retrieve the settings for the index specified in the request. You can quickly view some useful settings such as searchable attributes, attributes configured for faceting and many more.

Request: GET {{baseURL}}/1/indexes/{{indexName}}/settings

This query list all indices for the Algolia application id specific in the request header.

Request: GET {{baseURL}}/1/indexes?page=0&hitsPerPage=100

This query retrieve all API keys associated with the Algolia application. However, to execute this query you need an Admin API key. After executing this query for testing purposes, make sure to switch back to the Search API Key back in Postman.

Request: GET {{baseURL}}/1/keys

This query retrieve the logs as configured in query parameters but make sure to use the API key with logs ACL.

Query parameters:

  • indexName: Name of the index for which you want to fetch logs
  • length: Total number of entries in response
  • offset: This parameter let’s you skip a certain number of the most recent log entries
  • type: You can define the type of log entries you need in response. Default is all, remaining types are query, build and error
Request: GET {{baseURL}}/1/logs?offset=0&length=1&indexName={{indexName}}&type=all

I have created a curated list of queries that can help developers with day-to-day development and troubleshooting, along with a limited set of practical examples. If you’d like to explore additional Search API queries and capabilities, please refer to the Algolia documentation linked below:

https://www.algolia.com/doc/rest-api

Happy Learning!!!