API request failing

Highlighted
Excursionist
58 2 1

I am trying to fetch a REST API however I keep getting an error

 

My code:

 

const customers = async (accessToken, shop) => {
  const query = JSON.stringify(
      {
          "test"
        }
  });

  const response = await fetch(`https://${shop}/admin/api/2020-01/customers.json`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Shopify-Access-Token': accessToken,
    },
    body: query
  })

  const responseJson = await response.json();
    
};

module.exports = themeDownload;

 

The error is:

POST https://undefined/admin/api/2020-01/customers.json net::ERR_NAME_NOT_RESOLVED

 

0 Likes
Highlighted
Explorer
49 5 10

You have an extra closing curly bracket in the JSON.stringfy method,

plus the javascript object should be Key: value pairs this also might be the cause and i can see you're trying to create a customer so it should look something like this

const query = JSON.stringify({
  "customer": {
    "first_name": "Steve",
    "last_name": "Lastnameson",
    "email": "steve.lastnameson@example.com",
    "phone": "+15142546011",
    "verified_email": true,
    "addresses": [
      {
        "address1": "123 Oak St",
        "city": "Ottawa",
        "province": "ON",
        "phone": "555-1212",
        "zip": "123 ABC",
        "last_name": "Lastnameson",
        "first_name": "Mother",
        "country": "CA"
      }
    ]
  }
});

and also you can check the REST API docs to learn more https://shopify.dev/docs/admin-api/rest/reference/customers/customer#create-2020-01

H.Azzam
0 Likes
Highlighted
Excursionist
58 2 1

I have done that but it doesn't seem to get the {shop} and {accessToken} that was created when installing the app. 

This is the code that is inside the server.js file, how would I be able to get access to {shop} and {accessToken} from a different file? I hope that makes sense, I am new to all this so I have no idea what I am doing.

app.prepare().then(() => {
  const server = new Koa();
  const router = new Router();
  server.use(session({ secure: true, sameSite: 'none' }, server));
  server.keys = [SHOPIFY_API_SECRET_KEY];

  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      scopes: [''],
      async afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set('shopOrigin', shop, {
          httpOnly: false,
          secure: true,
          sameSite: 'none'
        });
        ctx.cookies.set('accessToken', accessToken);
                  console.log(`${shop}`);
        console.log(`${accessToken}`);
                  
        
        ctx.redirect('/');
      },
        
    }),
    
      
  );
0 Likes
Highlighted
Explorer
49 5 10

I don't know your project structure, but it seems you have a problem with Node.js itself not the API.

if you're trying to learn app development then i would suggest starting with the backend language first learn node.js, python, ruby or php first and then come back to the API it would make more sense for you then.

 

if you're not interested in app development and just playing around and wanna just get a test app up and running then i suggest following this shopify tutorial, to get started with your first app using Node.js https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-express

H.Azzam
0 Likes