Fetting data from builder

Hello team,
could you tell me please what is the best way to get data from builder,
do i always need to fetch builder to get specific data or there is built in hook or something for easy usage ?

Hello @aregkruitbosch,

Welcome to the Builder.io forum!

You can use the Content API to query and retrieve data from your models in Builder. The API supports advanced query filtering, allowing you to fetch exactly the data you need.

Depending on the Builder SDK you are using (React Gen 1 or Gen 2), here are examples to help you get started:

For Gen 1

Import builder and use the get() or getAll() helper:

import { builder } from '@builder.io/react';

const page = await builder.get('page', {
  fields: 'data.url, name',
});

const pages = await builder.getAll('page', {
  fields: 'data.url,name',
});

For Gen 2

Import fetchOneEntry() and fetchEntries() to fetch single and multiple entries, respectively. Note that the apiKey is a required field:

import { fetchOneEntry, fetchEntries } from '@builder.io/sdk-react';

const page = await fetchOneEntry({
  model: 'page',
  fields: 'data.url,name',
  apiKey: 'YOUR_API_KEY'
});

const pages = await fetchEntries({
  model: 'page',
  fields: 'data.url,name',
  apiKey: 'YOUR_API_KEY',});

For reference and example code for other SDK, you can refer to the below links.

Hope it helps!

Thanks,