How can I connect Liquid to the Shopify Admin API for a custom dashboard?

PrateektheDev
Shopify Partner
3 0 1

Actually, I am a developer and I want to make a custom dashboard for my customers where I want to connect my backend Admin API so that my customers can make crud operations with stocks, locations, running orders, etc, So in liquid how can I do it I want a possible scalable solution, the storefront is working fine but when it comes to connecting Admin API it's giving some errors like 401 unauthorized access. Please give me the best resources to work on it and a better Idea for this problem so I will have more clarity. Thanks

 

require('@shopify/shopify-api/adapters/node')
const { shopifyApi } = require("@shopify/shopify-api");
// const Shopify = require('@shopify/shopify-api').Shopify;

const shopify = shopifyApi({
    // The next 4 values are typically read from environment variables for added security
    apiKey: 'adminApiKey',
    apiSecretKey: 'adminApisecret',
    scopes: ['read_products', ...moreScopes],
    hostName: 'myShop.myshopify.com',
    adminApiAccessToken: "adminApiToken",
    isEmbeddedApp: false
    // ...
  }) 

app.get("/", async (req, res) => {
    // res.send("API RUNNING FINE BUDDY!")
    // const sessionId = shopify.utils
    // await shopify.session.getCurrentId({
    //     isOnline: true,
    //     rawRequest: req,
    //     rawResponse: res,
    //   });

    //   console.log(sessionId)
    
      // use sessionId to retrieve session from app's session storage
      // getSessionFromStorage() must be provided by application
    //   const session = await getSessionFromStorage(sessionId);
    
    //   const client = new shopify.clients.Graphql({
    //     session,
    //     // apiVersion: '',
    //   });

    // const response = await shopify.clients.Graphql({
        // data: `{
        //   products (first: 10) {
        //     edges {
        //       node {
        //         id
        //         title
        //         descriptionHtml
        //       }
        //     }
        //   }
        // }`,
    //   });

    // let sessionId = await shopify.session.customAppSession('myShop.myshopify.com')
    // let sessionId = shopify.session.customAppSession('myShop.myshopify.com')
    // let data = await shopify.utils.sanitizeShop('myShop.myshopify.com')
    // let data1 = await shopify.clients.Graphql({
    //     data: `{
    //         products (first: 10) {
    //           edges {
    //             node {
    //               id
    //               title
    //               descriptionHtml
    //             }
    //           }
    //         }
    //       }`,
    // })
    // console.log(shopify.hostName)
    const client = new shopify.clients.Rest('myShop.myshopify.com', 'adminApiToken');
    const products = await client.get({
        path: 'products',
    });
    res.json(products);

    // const client = shopify.clients.Rest({
    //     shopName: 'myShop.myshopify.com',
    //     accessToken: 'adminTokenHere'
    // });
    // const products = await client.get({
    //     path: 'products',
    // });
    // res.json(products);

    res.send( data1)
})

This is my code, the commented part are all the ways which I tried.
still I get 401 error
Reply 1 (1)

1080
Shopify Partner
293 8 42

@PrateektheDev  You have to use the app proxy option for the Shopify frontend call with your app here is link

App with the store integration here is link 
or Alternative  you can manage your way to auth those customer and allow the secure access the data.