Skip to content
Skip to main content
CONTACT SALESSTART BUILDING

GraphQL Content API

Builder Content

The GraphQL Content API lets you query your Builder content with GraphQL. Builder generates the schema from the Models in your Space, so each Model becomes a field that you can query.

To query your data using the GraphQL Content API, you can make requests to the endpoint with the following format:

https://cdn.builder.io/api/v3/graphql/YOUR_API_KEY?query=QUERY

You can also use POST requests with { "query": QUERY } as the body, though GET is preferred when possible for performance and caching purposes.

Explorer

The best way to explore the GraphQL API for your data is to use the GraphQL Explorer. The video below shows how to use it:

You can also open a GraphiQL view in your browser. Go to https://cdn.builder.io/api/v3/graphql/YOUR_API_KEY/view.

Querying Models

When using the GraphQL Content API, your Models are represented as fields defined on the root query type. For each Model, there are two corresponding fields:

  1. myCoolModel(...) to fetch paginated results
  2. oneMyCoolModel(...) to fetch a single record

For example, if your Space had Models named page, header, and footer, the root query type in your GraphQL schema would have fields such as page, onePage, footer, oneFooter, header, and oneHeader.

Models that aren't publicly readable appear in the schema only when your request includes an Authorization header with a Private API Key.

Arguments

Each Model field accepts these arguments:

ArgumentTypeDescription

query

JSON object

Filter results with MongoDB-style queries. For example, query: { data: { customNumberField: { $lt: 100 } } }.

target

JSON object

Pass targeting information. For example, target: { urlPath: "/foobar", device: "mobile" }.

options

JSON object

Pass additional API options. For example, options: { includeUnpublished: true }.

limit

Int

Limit the number of results. For example, limit: 10, offset: 10. The one fields don't take a limit argument; they always return one record.

offset

Int

Skip a number of results. For example, limit: 10, offset: 10.

sort

JSON object

Order results. For example, sort: { createdDate: -1 }.

Add ?cachebust=true to the request URL to skip cached results.

Examples

All of the following examples assume that your Space has a Model named page defined, but you could swap it out for any other Model you have defined.

Tip: Explore real-world examples of using the GraphQL API in Builder's open source examples, such as the Gatsby minimal starter.

Basic

Write a basic query to fetch the content of a Page:

query { page(limit: 1) { content } }

With targeting

Query with targeting to fetch a Page with a specific URL path:

query { page(target: { urlPath: "/foobar" }, limit: 1) { content } }

Visit the Content API documentation to learn more about custom targeting.

With query

Query with custom conditions to fetch a Page based on certain properties:

query { page(query: { data: { someProperty: "someValue" }}, limit: 1) { content } }

Visit the Content API documentation to learn more about custom querying.

For React

For rendering the queried data in a React application, use BuilderComponent:

query { page(query: { data: { someProperty: "someValue" }}, limit: 1) { content } } # Render with # <BuilderComponent name="page" content={data.page[0].content} />

Visit the Gatsby minimal starter to learn more about querying and rendering to React.

Get HTML

Query to retrieve HTML content for a Page:

query { page(options: { prerender: true }, limit: 1, target: { urlPath: "/" }) { data { html } } } # Render {{data.page[0].html}} in your site or app

Data model fields

Fetch data from the page Model requesting a single record limit: 1 where the value of the someProperty field is someValue. The response will include the value of the someProperty field from the retrieved record:

query { page(query: { data: { someProperty: "someValue" }}, limit: 1) { data { someProperty } } }

Errors

The API returns these messages:

StatusMessageCause

400

Organization is required

The URL doesn't include an API key.

404

No models found for this organization

Your Space has no Models that the request can read. Private Models appear only when the request includes an Authorization header with a Private API Key.

Any

There was an error processing your request

Your query has an error, for example an unknown argument or field. The API doesn't return details, so check the query against the arguments table.

What's next

For more information, visit the Content API documentation.

Was this article helpful?