You're reading "DatoCMS Site Search"

Integrating search on your site

Once you've configured your DatoCMS administrative area, from your website, you can make AJAX requests to our JSON API to present relevant results to your visitors.

Obtaining an API token

To do that, first you need to generate an API token with the proper permissions. Go to Settings > Roles and create a new role with just the Can perform Site Search API calls option checked:


You can then create a new API token associating it with the role you just created:


Awesome! Let's test if everything is working by making a curl request to our API endpoint:

curl '' \
  -H 'Authorization: API-Token YOUR_API_TOKEN_HERE' \
  -H 'Accept: application/json' \
  -I -X GET -s -S | head -n1

If the output is HTTP/1.1 200 OK, you're good to go!

Performing searches

To make it easy to integrate DatoCMS Site Search with your website, we make available a small JS library called datocms-search that you can add at the bottom of your pages:


    <!-- at the end of your page insert the following line -->
    <script src=""></script>

The library does not need any external dependencies, and can also be used with Webpack/Browserify. It exposes a DatoCmsSearch class that you can use to retrieve search results:

var client = new DatoCmsSearch("YOUR_API_TOKEN_HERE", "production");"florence")
  .then(function(response) {;
    // [
    //   {
    //     title: "The Crucifix by Brunelleschi",
    //     body: "...Chapel of Santa Maria Novella in <strong class=\"highlight\">Florence</strong>, dated to about 1410-1415. The Crucifix by Brunelleschi B..."
    //     url: "",
    //   },
    //   ...
    // ];
    // 42
  .catch(function(error) {

As you can see, possible matching text is already highlighted for you. You can change the surrounding HTML using the highlightWith option:"florence", { highlightWith: '<span class="h"></span>' })
  .then(function(response) {[0].body);
    // "...Chapel of Santa Maria Novella in <span class="h">Florence</span>, dated to about 1410-1415. The Crucifix by Brunelleschi B..."

You can also paginate results with the limit and offset options:"florence", { limit: 20, offset: 15 })

If you have a multi-language website, you can force to return only pages in a specific language with the locale option:"florence", { locale: "it" })

If you have a multi-language website and you don't pass any value for locale, DatoCMS will try its best to return pertinent results inspecting the Accept-Language header of the request to find a matching language for the user visiting the page.

Go ahead to: Search widget
Feel like something is missing in this page?
, submit an issue or propose a change on Github!