Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
Hi Everyone,
I am trying to aceess location through webhook, but no idea why it is not able to register. Although the same code is working for other Webhooks and scopes or events like ('read_inventory', 'write_inventory' OR 'read_products', 'write_products',) BUT for 'read_locations' it is not working-------------------------------------------
I searched about it so many places but not able to fix it. Hope someone will help me to fix . For your reference i am adding some snip and the resulted error console.
Thanks....
---server.js File---- require('isomorphic-fetch'); const Koa = require('koa'); const next = require('next'); const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth'); const dotenv = require('dotenv'); const { verifyRequest } = require('@shopify/koa-shopify-auth'); const session = require('koa-session'); const request = require('request'); dotenv.config(); const { default: graphQLProxy } = require('@shopify/koa-shopify-graphql-proxy'); const Router = require('koa-router'); const {receiveWebhook, registerWebhook} = require('@shopify/koa-shopify-webhooks'); const { ApiVersion } = require('@shopify/koa-shopify-graphql-proxy'); 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_locations' ], async afterAuth(ctx) { const { shop, accessToken } = ctx.session; ctx.cookies.set('shopOrigin', shop, { httpOnly: false }); const registration = await registerWebhook({ address: `${HOST}/webhooks/locations/create`, topic: 'LOCATIONS_CREATE', accessToken, shop, apiVersion: ApiVersion.October19 }); if (registration.success) { console.log('Successfully registered LOCATIONS webhook!'); } else { console.log('Failed to register webhook', registration.result); }; await getSubscriptionUrl(ctx, accessToken, shop); // don't worry about it }, }), ); const webhook = receiveWebhook({secret: SHOPIFY_API_SECRET_KEY});
router.post('/webhooks/locations/create', webhook, (ctx)=>{ console.log(ctx.state.webhook) console.log('your location payload is here....', ctx.state.webhook.payload) })
server.use(graphQLProxy({version: ApiVersion.October19}));
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}`); }); });
================ consoled result is here ===============
Failed to register webhook {
data: {
webhookSubscriptionCreate: { userErrors: [Array], webhookSubscription: null }
},
extensions: {
cost: {
requestedQueryCost: 10,
actualQueryCost: 10,
throttleStatus: [Object]
}
}
}
The raw response would be helpful. Anyways, the topic should be locations/create not LOCATIONS_CREATE.
Hi,
Thank you for your reply, as you told to make
topic :'locations/create'
i did that thing also but it is not working , and at thr same time if you will check
shopify tutorial page
https://github.com/Shopify/shopify-demo-app-node-react/blob/master/server.js
or
koa-shopify-webhook github page
https://github.com/Shopify/quilt/blob/master/packages/koa-shopify-webhooks/README.md
they have written like that only for different topics like 'PRODUCTS_CREATE' or 'ORDERS_CREATE' .
so may be there are some other problem .. if it comes to you must reply here..
Thanks...
Ah ok, missed you are using app node, LOCATIONS_CREATE is correct. Can you post the raw response you are getting?
yeah ... that thing only i am getting as a response, in console
Failed to register webhook { data: { webhookSubscriptionCreate: { userErrors: [Array], webhookSubscription: null } }, extensions: { cost: { requestedQueryCost: 10, actualQueryCost: 10, throttleStatus: [Object] } } }
In my knowledge, why should it matter. anyway it is working for other scopes or topics . only location webhook i am not able to register.
🙂 it should matter because you can find the error message there, hence my question.
so what should i do ?
Can you add a code that would iterate through userErrors array and print the values if any, or something like that:
console.log('Failed to register webhook', registration.result.data.webhookSubscriptionCreate.userErrors.join(','));
it's coming like that...
Failed to register webhook [object Object]