Search by Algolia
What is end-to-end AI search?
ai

What is end-to-end AI search?

Simplicity is critical for search engines, but building one that enables that simplicity is complex.  Over the last 20+ years ...

Abhijit Mehta

Director of Product Management

Comparing AI search solutions in a crowded market landscape
ai

Comparing AI search solutions in a crowded market landscape

Many new AI-powered search solutions have been released this year, and each promises to provide great results, but as ...

Andy Jones

Marketing Campaign Production Manager

What is B2B ecommerce? Everything you need to know
e-commerce

What is B2B ecommerce? Everything you need to know

When you think of “customer experience,” what comes to mind? People, right? Specifically, consumers. Retail customers. That’s easy; the ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

What is ecommerce merchandising? Key components and best practices
e-commerce

What is ecommerce merchandising? Key components and best practices

A potential customer is about to land on the home page of your ecommerce platform, curious to see what cool ...

Catherine Dee

Search and Discovery writer

AI-powered search: From keywords to conversations
ai

AI-powered search: From keywords to conversations

By now, everyone’s had the opportunity to experiment with AI tools like ChatGPT or Midjourney and ponder their inner ...

Chris Stevenson

Director, Product Marketing

Vector vs Keyword Search: Why You Should Care
ai

Vector vs Keyword Search: Why You Should Care

Search has been around for a while, to the point that it is now considered a standard requirement in many ...

Nicolas Fiorini

Senior Machine Learning Engineer

What is AI-powered site search?
ai

What is AI-powered site search?

With the advent of artificial intelligence (AI) technologies enabling services such as Alexa, Google search, and self-driving cars, the ...

John Stewart

VP Corporate Marketing

What is a B2B marketplace?
e-commerce

What is a B2B marketplace?

It’s no secret that B2B (business-to-business) transactions have largely migrated online. According to Gartner, by 2025, 80 ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

3 strategies for B2B ecommerce growth: key takeaways from B2B Online - Chicago
e-commerce

3 strategies for B2B ecommerce growth: key takeaways from B2B Online - Chicago

Twice a year, B2B Online brings together industry leaders to discuss the trends affecting the B2B ecommerce industry. At the ...

Elena Moravec

Director of Product Marketing & Strategy

Deconstructing smart digital merchandising
e-commerce

Deconstructing smart digital merchandising

This is Part 2 of a series that dives into the transformational journey made by digital merchandising to drive positive ...

Benoit Reulier
Reshma Iyer

Benoit Reulier &

Reshma Iyer

The death of traditional shopping: How AI-powered conversational commerce changes everything
ai

The death of traditional shopping: How AI-powered conversational commerce changes everything

Get ready for the ride: online shopping is about to be completely upended by AI. Over the past few years ...

Aayush Iyer

Director, User Experience & UI Platform

What is B2C ecommerce? Models, examples, and definitions
e-commerce

What is B2C ecommerce? Models, examples, and definitions

Remember life before online shopping? When you had to actually leave the house for a brick-and-mortar store to ...

Catherine Dee

Search and Discovery writer

What are marketplace platforms and software? Why are they important?
e-commerce

What are marketplace platforms and software? Why are they important?

If you imagine pushing a virtual shopping cart down the aisles of an online store, or browsing items in an ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

What is an online marketplace?
e-commerce

What is an online marketplace?

Remember the world before the convenience of online commerce? Before the pandemic, before the proliferation of ecommerce sites, when the ...

Catherine Dee

Search and Discovery writer

10 ways AI is transforming ecommerce
e-commerce

10 ways AI is transforming ecommerce

Artificial intelligence (AI) is no longer just the stuff of scary futuristic movies; it’s recently burst into the headlines ...

Catherine Dee

Search and Discovery writer

AI as a Service (AIaaS) in the era of "buy not build"
ai

AI as a Service (AIaaS) in the era of "buy not build"

Imagine you are the CTO of a company that has just undergone a massive decade long digital transformation. You’ve ...

Sean Mullaney

CTO @Algolia

By the numbers: the ROI of keyword and AI site search for digital commerce
product

By the numbers: the ROI of keyword and AI site search for digital commerce

Did you know that the tiny search bar at the top of many ecommerce sites can offer an outsized return ...

Jon Silvers

Director, Digital Marketing

Using pre-trained AI algorithms to solve the cold start problem
ai

Using pre-trained AI algorithms to solve the cold start problem

Artificial intelligence (AI) has quickly moved from hot topic to everyday life. Now, ecommerce businesses are beginning to clearly see ...

Etienne Martin

VP of Product

Looking for something?

facebookfacebooklinkedinlinkedintwittertwittermailmail

In this article, we’ll look into how to integrate Sanity.io with Algolia. Sanity.io is a content platform that comes with an open source headless CMS that you can customize, and a hosted real-time datastore that lets you read and update data. The philosophy behind Sanity.io is that you should be able to treat content as data and bring it to whatever product or application you have.

Now, a CMS normally doesn’t come with search functionality. It’s not easy to build even a minimal search function on top of it by yourself, let alone something that includes important features like typo tolerance and faceting. This is what our Algolia plug-in does: it adds a flexible and feature-rich search functionality on top of your CMS.

CMS<>Algolia using webhooks

There are many ways to integrate a CMS with Algolia. One of the most efficient ways is to leverage webhooks. You can create an API as a webhook endpoint and configure your CMS to trigger the webhook when content changes. In your API, you can create, update, or delete the corresponding record in your index at Algolia.

You therefore need to create an API as a webhook. If you have a web server, you can add an API there. Or if you have Sanity Studio hosted, for example, on Netlify or Vercel, you can write a serverless function as your API.

However, this part can be cumbersome. In the API, you need to check why a webhook request is triggered: creation, update, or deletion of a record. After that, you need to call the corresponding method to apply the change to Algolia. It’s not a big chunk of code, but it can be time-consuming if you don’t get the data schema right. That’s where sanity-algolia comes in and helps us deal with both sides.

A better way: sanity-algolia (with tutorial)

With sanity-algolia, you need to transform the payload for your Algolia index.

Here is how.

Install the dependencies:

npm install algoliasearch sanity-algolia

# or

yarn add algoliasearch sanity-algolia

Create the API:

import algoliasearch from 'algoliasearch';
import sanityClient from '@sanity/client';
import indexer, { flattenBlocks } from 'sanity-algolia';

const algolia = algoliasearch(...);
const sanity = sanityClient(...);

export default function handler(req, res) {
  const sanityAlgolia = indexer(
    {
      post: {
        index: algolia.initIndex('posts'),
      },
    },
    document => {
      switch (document._type) {
        case 'post':
          return {
            title: document.title,
            path: document.slug.current,
            publishedAt: document.publishedAt,
            excerpt: flattenBlocks(document.excerpt),
          };
        default:
          throw new Error(`Unknown type: ${document.type}`);
      }
    }
  );

  return sanityAlgolia
    .webhookSync(sanity, req.body)
    .then(() => res.status(200).send('ok'));
}

Let’s say the endpoint of this API is deployed at

https://my-example/api/update-algolia

Visit your Sanity dashboard to add the webhook.

Now whenever content changes in your Sanity studio, it will trigger this webhook, which will update your Algolia index with the changes.

Thanks to the library sanity-algolia, if you define which properties to fetch and how to transform them, the rest is automatically done for you.

To learn more, visit this GitHub repo.

About the author
Eunjae Lee

Recommended Articles

Powered byAlgolia Algolia Recommend

How can you improve your content management system (CMS) with great search?
product

Catherine Dee

Search and Discovery writer

What is a headless CMS (content management system)?
product

Catherine Dee

Search and Discovery writer

Choosing your APIs for Jamstack
engineering

Matthew Foyle
Sarfaraz Rydhan

Matthew Foyle &

Sarfaraz Rydhan