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
164 23 27

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 on (liushuaicc@shinetechsoftware.com)