Home App Docs Blog Github

How do I query Builder content based on data that might be set on blocks or components within the content entry or page?

With Builder you can set top level properties on models and query on those properties. For example you could have a setting on a model that signifies the locale or language of any entry within that model. for example:

// api call
https://cdn.builder.io/api/v2/content/banner?apiKey=your-api-key&query.data.locale=en

// or setting query option on builder
   builder.get('banner', {
        query: {
            data: {
                locale: 'en'
            },
        },
    });

But what if you want to query Builder content based on the data contained in blocks or components, not data that is set on the top level of the content entry? You can do that by nesting the query, as shown in this mongodb doc. What does it look like in Builder land? Let’s say you want to pull all content entries from the banner model that contain a component or block that has a property set on it called newTab. You can do that by drilling down to where that piece of data lives within the structure of the builder content, and doing a query like this:

// api call
https://cdn.builder.io/api/v2/content/banner?apiKey=your-api-key&query.data.blocks.component.options.newTab=true

// or in the builder SDK version
const banner = await builder
  .get('banner', {
    query: {
      data: {
        blocks: {
          component: {
            options: {
              newTab: true,
            },
          },
        },
      },
    },
  })
  .promise();

Looking at the structure of your content entries to determine what the query should be is very helpful. Using the rest api explorer or just hitting the endpoint with a generic non filtered api call (e.g. https://cdn.builder.io/api/v2/content/your-model-name?apiKey=your-api-key) will allow you to see the structure of the data so that you are able to build the proper query.

1 Like