skip to Main Content

UPDATE: I have this Private API request to get a customer metafield data that sends a request to the shopify admin.

const {getAccessToken} = require('./auth')
    const request = require('request')
    const {shopFromPermanentDomain} = require('./helpers')

const getCustomerMetafields = ({accessToken, id, shop}) => new Promise((resolve, reject) => {
  request({
    url:`https://${shop}.myshopify.com/admin/customers/${id}/metafields.json',
    headers: {
      'X-Shopify-Access-Token': accessToken
    }
  }, (error, response, body) => {
    const {errors} = JSON.parse(body)

    if (response.statusCode === 200) {
      const { metafields } = JSON.parse(body)
      resolve(metafields)
    }

    reject({error: errors, status: 500})
  })
})

const getCustomerMetafieldsResponse = (req, res) => {
  const {id, permanentDomain} = req.body

  if (id && permanentDomain) {
    const shop = shopFromPermanentDomain(permanentDomain)

    getAccessToken({shop})
      .then(accessToken => getCustomerMetafields({accessToken, id, shop})
        .then(meta => res.json({
          meta,
          status: 200
        }))
      )
      .catch(({error, status}) => res.json({error, status}))
  } else {
    res.json({error: 'Missing params', status: 500})
  }
}

module.exports = getCustomerMetafieldsResponse

I make this request from the front-end to my API with the following request.

const getCustomerMeta = ({
   id,
   permanentDomain
}) => new Promise((resolve, reject) => {
  post({
    params: { email, permanentDomain, task: 'get-customer-meta' },
    then: ({ error, id, state, status }) => {
      if (status === 200) {
        resolve({ id, state })
      }

      reject(error)
    },
    url: '/apps/spoke'
  })
})
    getCustomerMeta({
       id, // 98303739294 (Customer ID)
       permanentDomain // "store.myshopify.com"
    })

When making this request, I get the following request error:

 500 (Internal Server Error)

VM706781:8 Uncaught SyntaxError: Unexpected token < in JSON at position 7
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.l.onload

I then want to GET the customer metafields data so that I can populate the front-end with the data gathered.

Thanks!

2

Answers


  1. You cannot call the back-end, ie) /admin, from the front-end, without using an App Proxy. On the front-end, the code you’d write would make a Proxy XHR call to your App, with the Customer ID. Using that ID, you’d then get the Metafields for the Customer resource, not the Shop as per your example. With the Metafields resource for the customer, you could then look for the ones that interest you, and draw them on the front-end.

    Or just render the metafields for the logged in customer, using Liquid, as that is much simpler.

    Login or Signup to reply.
  2. The api has probably changed drastically since you asked this question in 2018, but now you can get the metafields for the customer via the customers or metafields endpoints.

    e.g.

    /admin/api/{api-version-number}/customers/{shopify-customer-id}/metafields.json
    

    directy to the metafields endpoint would be:

    /admin/api/{api-version-number}/metafields.json?metafield[owner_id]={shopify-customer-id}&metafield[owner_resource]=customers
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search