How to pass access token and shop name to Shopify API Node new object

Solved
The1987
Shopify Partner
24 3 5

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)
                );

            })

 

 

 

 

Accepted Solution (1)

Accepted Solutions
The1987
Shopify Partner
24 3 5

This is an accepted solution.

Yes, I received an answer. Thank you.

View solution in original post

Replies 2 (2)
Alex
Shopify Staff
Shopify Staff
1561 81 337

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

The1987
Shopify Partner
24 3 5

This is an accepted solution.

Yes, I received an answer. Thank you.