Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Troubleshooting GraphQL API authentication with Axios in Slack thread

5 replies
Last updated: Apr 28, 2020
Is there anyone that successfully have fetched data with DocumentFilter
is_draft: true
from graphql api outside the graphql playground?
Apr 28, 2020, 6:56 PM
Are you sure the requests are authenticated?
Apr 28, 2020, 7:05 PM
I pass a
Authorization
header with bearer tokenlike this


const response = await <http://axios.post|axios.post>(`${process.env.GRAPHQL_URL}`, {
		query: print(query),
		variables: variables,
		headers: {
			'Content-Type': 'application/json',
			'Authorization': `Bearer ${process.env.GRAPHQL_TOKEN}`
		}
	});
Apr 28, 2020, 7:15 PM
What kind of token is the GRAPHQL_TOKEN?
Apr 28, 2020, 7:31 PM
it's token of type read, but I think the issue is in my code, I've noticed that postman have support for graphql &amp; I could successfully do the request in there, so trying to figure out a way around with axios
Apr 28, 2020, 7:36 PM
Ok,change the code to the following did the trick, thanks anyway!

const response = await axios({
		method: 'post',
		url: `${process.env.GRAPHQL_URL}`,
		headers: {
			'Content-Type': 'application/json',
			Authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`
		},
		data: {
			query: print(query),
			variables: variables,
		}
	});
Apr 28, 2020, 7:42 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?