Content Delivery API > Your first request

Your first request

In REST, HTTP verbs determine the operation performed. In GraphQL, you'll provide a JSON-encoded body even if you're performing a query operation, so the HTTP verb is always POST.

Curl example
$ curl 'https://graphql.datocms.com/' \
-H 'Authorization: YOUR-API-TOKEN' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data-binary '{ "query": "query { allPosts { title } }" }'
Vanilla JS
fetch(
'https://graphql.datocms.com/',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${process.env.DATOCMS_READONLY_TOKEN}`,
},
body: JSON.stringify({
query:
}),
}
)
.then(res => res.json())
.then((res) => {
console.log(res.data)
})
.catch((error) => {
console.log(error);
});
@datocms/cda-client

We also offer a lightweight, TypeScript-ready package that offers various helpers around the native Fetch API to perform GraphQL requests towards DatoCMS Content Delivery API:

import { executeQuery } from '@datocms/cda-client';
const result = await executeQuery('{ allPosts { title } }', {
token: process.env.DATOCMS_READONLY_TOKEN,
});
console.log(result);

You can learn more about this package on it's README file.

Pro tip: Top 5 JavaScript GraphQL Client Libraries

This blog post ranks the best JavaScript GraphQL client libraries, helping you choose the right tool based on your project’s specific needs and ensuring efficient and optimized GraphQL data fetching.