What is your developer-dependent CMS actually costing you? Calculate your costs and get a full report
What is your developer-dependent CMS actually costing you? Calculate your costs and get a full report
APIs
Request your Agility CMS content using GraphQL: endpoints, authentication, what's in the schema, filtering, caching, and when to use GraphQL instead of the Fetch API.
Every Agility instance has a GraphQL API alongside the REST Content Fetch API. Its schema is generated from your content models, so each content list you create is immediately queryable, with a type for every field. It serves both published and staging (preview) content, in every locale.
Compared with the Content Fetch (REST) API, GraphQL gives you:
This article covers calling the Agility GraphQL API directly. For end-to-end TypeScript types in a Next.js site, see Typed GraphQL with TypeScript and Next.js.
Created by Facebook in 2012 and open-sourced in 2015, GraphQL is a query language for APIs. The client describes exactly the data it needs, and the response has exactly that shape: no over- or under-fetching, and data from several sources in a single request.
This 30-second clip from our friends at Shopify shows the difference from a traditional REST API:
fetch.POST https://api.aglty.io/v1/{instance-guid}/{fetch|preview}/{locale}/graphql
| Part | Value |
|---|---|
fetch / preview | fetch returns published content; preview returns the latest staging version |
{locale} | A locale code from your instance, such as en-us. Each locale has its own endpoint |
| Header | apikey: <your API key>. Use the Fetch key with fetch and the Preview key with preview |
Find your instance GUID and API keys in Settings > API Keys.
The GraphQL endpoint uses /v1/ and the apikey header; the REST Fetch API uses neither (https://api.aglty.io/{guid}/fetch/{locale}/... with an APIKey header). A request to a route that doesn't exist returns Invalid API type (must be fetch or preview), which means the URL is wrong, not the key.
const res = await fetch(`https://api.aglty.io/v1/${guid}/fetch/en-us/graphql`, {
method: "POST",
headers: { "Content-Type": "application/json", apikey: fetchApiKey },
body: JSON.stringify({
query: `{
offices(take: 50, sort: "fields.city") {
contentID
fields { title city zip }
}
}`,
}),
})
const { data, errors } = await res.json()
| Root field | Returns |
|---|---|
One field per content list, named after the list's reference name in lower case (for example offices, newsreleases) | The list's items: contentID, properties (versionID, modified, state, itemOrder...), seo, and fields, with one typed field per model field |
ag_sitemapflat(channelName) | The flat sitemap: path, pageID, title, menuText, visible, redirecturl, isFolder |
ag_sitemapnested(channelName) | The nested sitemap |
ag_pages(pageID) | Pages: name, path, title, menuText, templateName, seo, visible, dynamic, properties and zones |
ag_urlredirections | URL redirections: originUrl, destinationUrl, statusCode |
Things to know:
null (for example, an optional field left blank).zones return each component's module name and its item.fields as JSON. Content lists are fully typed; the fields of components placed on pages are not. For typed component data, read it with the Fetch API or query the content list it links to.Every instance has a GraphQL playground for trying queries. Go to Settings > API Keys and click Explore GraphQL API.
Update the endpoint URL with your locale and fetch or preview:

Then click Set Headers and add your API key as the apikey header. Use the Fetch key for published content and the Preview key for staging content:


Click Save, then Reload to load the schema:

Open Docs and click Root to see everything you can query in your instance.
List fields take take, skip, sort, direction and filter, plus contentID to get a single item:
{
articles(take: 10, skip: 0, sort: "fields.date", direction: "desc", filter: "fields.title[like]\"Person\"") {
contentID
properties { versionID }
fields { title date }
}
}
filter uses the same operators as the REST API (eq, ne, lt, gt, in, contains, like, with AND / OR and parentheses). See GraphQL & Rest API Filtering.
preview endpoint and Preview key only for preview sessions, and don't cache those responses.| Use GraphQL when | Use the Fetch API / SDK when |
|---|---|
| You want typed, field-level control over content lists | You're rendering pages and their components (the SDKs handle this) |
| One view needs several lists at once | You need the Content Sync API or the SDKs' page helpers |
| You're building outside JavaScript, or without an SDK | The SDK already gives you what you need |
Most sites use both: the SDK for pages, and GraphQL for typed content lists.