Help with basic test: get orders pending using API call in nodejs (from the tutorial)

Highlighted
New Member
12 0 0

I'm building an app based on the tutorial app like many others posting questions here, and now I need to add a feature to get orders and add specific tags based on the pending status.

The api documentation says I need to make a get request, and on the docs it gives this piece of code:

GET /admin/api/#{api_version}/orders.json?financial_status=pending

I just have no idea how to make this call, where to wrap this code etc.

Could someone care to help me put the code above into the server.js code below from the tutorial ?

require('isomorphic-fetch');
const dotenv = require('dotenv');
dotenv.config();
const Koa = require('koa');
const next = require('next');
const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
const { verifyRequest } = require('@shopify/koa-shopify-auth');
const session = require('koa-session');
const { default: graphQLProxy } = require('@shopify/koa-shopify-graphql-proxy');
const { ApiVersion } = require('@shopify/koa-shopify-graphql-proxy');
const Router = require('koa-router');
const { receiveWebhook, registerWebhook } = require('@shopify/koa-shopify-webhooks');
const getSubscriptionUrl = require('./server/getSubscriptionUrl');

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

const {
  SHOPIFY_API_SECRET_KEY,
  SHOPIFY_API_KEY,
  HOST,
} = process.env;

app.prepare().then(() => {
  const server = new Koa();
  const router = new Router();
  server.use(session(server));
  server.keys = [SHOPIFY_API_SECRET_KEY];

  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      scopes: ['read_products', 'write_products'],
      async afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set("shopOrigin", shop, { httpOnly: false });

        const registration = await registerWebhook({
          address: `${HOST}/webhooks/products/create`,
          topic: 'PRODUCTS_CREATE',
          accessToken,
          shop,
        });

        if (registration.success) {
          console.log('Successfully registered webhook!');
        } else {
          console.log('Failed to register webhook', registration.result);
        }
        await getSubscriptionUrl(ctx, accessToken, shop);
      }
    })
  );

  const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });

  router.post('/webhooks/products/create', webhook, (ctx) => {
    console.log('received webhook: ', ctx.state.webhook);
  });
  //where does this go?? /admin/api/#{api_version}/orders.json?financial_status=pending

  server.use(graphQLProxy({ version: ApiVersion.April19 }));

  router.get('*', verifyRequest(), async (ctx) => {
    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
  });

  server.use(router.allowedMethods());
  server.use(router.routes());

  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
  });
});
0 Likes
Shopify Staff
Shopify Staff
472 35 90

Hey @zoultrex,

 

You are mixing up the fundamentals of interacting with the API a little bit with a tutorial that is slightly more advanced.  The line from the docs you are looking at: 

GET /admin/api/#{api_version}/orders.json?financial_status=pending

is more of indication of which endpoint (orders.json), query (?financial_status=pending) and method (HTTP GET) to use with our REST API in order to get the information you want back from Shopify.  You won't be able to simply plug that into Node&React tutorial app.  Additionally the quoted request is for our REST API, and you've only imported a module to make GraphQL calls in your server.js (which is the only type of request covered by that tutorial).

 

I'd suggest taking a step back to look into the basics of making calls to the API, perhaps with an HTTP client such as Postman or Insomnia to get started with making API calls manually to the REST API.

 

Hope that helps get you on track a little,

Ryan

 

Developer Experience @ Shopify
0 Likes