Development discussions around Shopify APIs
I posted on Stackoverflow and wanted to ask the community for help as well.
I am building a public shopify app and I want to add a POST route that allows a metafield to be created.
In the shopify-api-node module the following is stated:
accessToken - Required for public apps - A string representing the permanent OAuth 2.0 access token. This option is mutually exclusive with the apiKey and password options. If you are looking for a premade solution to obtain an access token, take a look at the shopify-token module."
Here is the object that needs the shopName and accessToken
const shopify = new Shopify({ shopName: 'your-shop-name', accessToken: 'your-oauth-token' });
In the Shopify Node / Express documentation it has you add in /shopify/callback route qwhich includes the the Oauth:
// Shopify Callback Route // app.get('/shopify/callback', (req, res) => { const { shop, hmac, code, state } = req.query; /// ... skipping over code ... /// request.post(accessTokenRequestUrl, { json: accessTokenPayload }) .then((accessTokenResponse) => { const accessToken = accessTokenResponse.access_token; // DONE: Use access token to make API call to 'shop' endpoint const shopRequestUrl = 'https://' + shop + '/admin/api/2019-04/shop.json'; const shopRequestHeaders = { 'X-Shopify-Access-Token': accessToken, }; }); /// ... skipping over code ... /// });
Instead of using the shopify-token module can I access/should I access this information from the /shopify/callback route in the following manner (see below)? Or is there a better way to do this / can you provide examples?
Server.js
// Declare new global variables // var accessTokenExport; var shopExport; // New Function // function exportTokens(accessToken) { accessTokenExport = accessToken; shopExport = shop; }
// Shopify Callback Route // app.get('/shopify/callback', (req, res) => { // Export variables to New Function exportTokens(shop, accessToken); });
// New POST route // app.post("/api/createMetafield", function (req, res) { const shopify = new Shopify({ shopName: shopExport, accessToken: accessTokenExport }); shopify.metafield.create({ key: 'warehouse', value: 25, value_type: 'integer', namespace: 'inventory', owner_resource: 'metafield', // owner_id: 632910392 }).then( metafield => console.log(metafield), err => console.error(err) ); })
Solved! Go to the solution
This is an accepted solution.
It looks like you were provided an answer that was flagged as accepted on stack overflow. Can you confirm that you have what you need now?
Cheers.
Alex | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
This is an accepted solution.
User | RANK |
---|---|
43 | |
16 | |
9 | |
9 | |
7 |
Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023One of the key components to running a successful online business is having clear and co...
By Ollie Mar 6, 2023