Algolia Search with Svelte

This tutorial is an extension of the [Algolia Cloud Functions Guide](/lessons/algolia-cloud-functions/). You must have the Cloud Functions deployed to start making instant search queries from your frontend app.

Install Algolia

Algolia does not provide official support for Svelte, but we can easily implement our own custom UI with AlgoliaSearch.

Note. You can also use InstantSearch.js if you want pre-built components.

command line
npm install algoliasearch

Update the index.html

The Algolia client makes a reference to process, which does not exist in the browser. Add the code below to prevent errors.

file_type_html public/index.html
	<title>Svelte app</title>

	<!-- ADD THIS LINE -->
	<script>window.process = {env: { DEBUG: undefined } }</script>

Svelte Algolia Search Component

The component below initializes the AlgoliaSearch lite, then binds a request to each keyup event on a form input. Algolia returns an object with the resulting hits, which are updated on the component’s hit property.

import { onMount } from 'svelte';
import algoliasearch from 'algoliasearch/lite';

let searchClient;
let index;

let query = '';
let hits = [];

onMount(() => {

	searchClient = algoliasearch(

	index = searchClient.initIndex('customers');

	// Warm up search{ query }).then(console.log)


// Fires on each keyup in form
async function search() {
	const result = await{ query });
	hits = result.hits;


	:global(em) {
		color: red;
		font-weight: bold;
		background: black;

<h1>Svelte InstantSearch</h1>

	<input type="text" bind:value={query} on:keyup={search}>

{#each hits as hit}
	<img src={hit.avatar} alt={hit.username}>
		<h3>{hit.username} {hit.objectID}</h3>
		<!-- <p>{}</p> -->

		<p contenteditable bind:innerHTML={}></p>

Demo of Algolia Search in Svelte 3

Demo of Algolia Search in Svelte 3


Ask questions via GitHub below OR chat on Slack #questions