Connecting to GraphQL sandbox environment from Next.JS

I am trying to connect to my CMS’ sandbox environment from a Next.JS application using graphql-request, but everything I do makes it 401.

Connecting to the main environment works just fine.

I am using my sandbox URL (
My headers look like this:

const headers = {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${API_TOKEN}`,
    Environment: `main-copy-2021-11-22`

I am sure the API token is correct, because it works fine with the main environment, and the API keys are identical for both.

Is there something I am missing?

The error it returns is:

1 Like

Hello @GC-Josh

You can use this documentation page for an example on what headers to use. You can see that the environment header is called “X-Environment” (also, remember to use JSON.stringify(…) on your headers)

And this documentation page to see how you can handle requests made like that with graphQL.

Remembering you can always use the datocms-client package to set up a sandbox client with ease using:

const client = new SiteClient(
  { environment: 'my-sandbox' },

Let me know if it works!

1 Like

Thank you for the suggestion @m.finamor

It turns out that the API key I was using was only valid for the main environment, but not the sandbox environment. I got a new one and that worked just fine.