Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Customer Account API making requests via API

Customer Account API making requests via API

Kkkkk3
Shopify Partner
1 0 0

I am using Customer Account API to use customer account in my application.

Considering the docs, it says I should use Access Token to make requests to gql, but I am always have this error:

"Not a valid access token".

 

They also have the mistake in the example:

 

 

const response = await fetch(
'https://shopify.com/<shop-id>/account/customer/api/2024-07/graphql',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: {access_token},
    },
    body: JSON.stringify({
      operationName: 'SomeQuery',
      'query { customer { emailAddress { emailAddress }}}',
      variables: {},
    }),
  },

This code will never work, what is happening in the request body..??

 

 

This is how I try to make requests:

 

	query(query) {
		if (!query) return;
		const headers = {
			'Content-Type': 'application/json',
			Authorization: `${this.cookies.get('auth-token')}`,
		};

		console.debug(headers);

		return this.fetch(this.baseUrl, {
			method: 'POST',
			headers,
			body: JSON.stringify({ query }),
		});
	}

And my headers contain:

 

 

{
  'Content-Type': 'application/json',
  Authorization: 'atkn_****_****_****'
}

API response in this case:

 

 

{ errors: [ { message: 'Not a valid access token' } ] }

I also have access token of authenticated user, and other stuff that docs described before "Endpoint and queries" section. 

So how to make authenticated request to get my authenticated customer data?

 

Reply 1 (1)

Kyle_liu
Shopify Partner
424 53 76

Hi @Kkkkk3 

 

Use 'X-Shopify Access Token' instead of 'Authorization'

const response = await fetch(
'https://shopify.com/<shop-id>/account/customer/api/2024-07/graphql',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Shopify-Access-Token': {access_token},
    },
    body: JSON.stringify({
      operationName: 'SomeQuery',
      'query { customer { emailAddress { emailAddress }}}',
      variables: {},
    }),
  },

 

If this is helpful, please Like and Accept the solution.
Want to modify or custom changes on store? Let me help.
- Feel free to contact me Email Me Buy Me A Coffee