shopify_deno
Shopify Integration for Deno.
How to use
This library has an abstraction of the application and the use of the API.
Creating the application instances
This implementation allows several applications in the same server instance in a simple way. Routes are automatically generated.
Example:
import { req, res, Server } from "https://deno.land/x/faster/mod.ts";
import {
ShopifyAPI,
ShopifyApp,
WebHookCall,
} from "https://deno.land/x/shopify_deno/mod.ts";
const server = new Server();
server.get(
"/my_app_1_index", //shopifyApp1 home route
async (ctx: any, next: any) => {
ctx.res.body = "My Shopify Deno";
await next();
},
);
const shopifyApp1 = new ShopifyApp(
{
api_key: "79e4871756c98ccf48ac647c724022e1",
api_secret: "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",
scopes: "read_products,read_orders",
host: "https://xxx.ngrok.io", //Without ending with "/"
namespace: "my_app_1_ns", //you can instantiate different apps on the same server with different namespaces
home_route: "/my_app_1_index",
webhooks: [ //OPTIONAL
{
topic: "orders/create",
func: (hook: WebHookCall) => {
//avoid using await here, prevent shopify webhook timeout is important
console.log(hook.data);
console.log(hook.shop);
},
},
],
},
server,
);
await server.listen({ port: 80 });
See type “ShopifyAppOptions” for full options.
Configure auto renerated routes
In Shopify Configs, configure:
- App URL:
"host"/"namespace"/install
- Allowed redirection URL(s):
"host"/"namespace"/auth
"host"/"home_route"
- Privacy policy:
"host"/"namespace"/privacy_policy
- Customer data request endpoint:
"host"/"namespace"/webhooks/client_data
- Customer data erasure endpoint:
"host"/"namespace"/webhooks/delete_client
- Shop data erasure endpoint:
"host"/"namespace"/webhooks/delete_shop
Each webhook is automatically placed in (for information only, the library registers automatically with the api):
"host"/"namespace"/webhooks/"topic"
Using the API
This library abstracts the communication and handles the “API rate limit”.
Examples:
const access_token = await shopifyApp1.getAccessToken(shop);
const api = new ShopifyAPI(shop, access_token); // 'shop' it's something like myexampleshop.myshopify.com, 'shop' and 'access_token' comes from 'userTokenFunc'
const data1 = await api.graphQL(`
{
products(first: 10, query:"tag:*(MY_TAG1)*"){
edges{
node {
tags
}
}
}
}
`);
const data2 = await api.get(
`/admin/api/2021-01/script_tags.json`,
);
const data3 = await this.post(`/admin/api/2021-01/script_tags.json`, {
"script_tag": {
"event": "onload",
"src": "https://xxx.ngrok.io/myscript.js",
"cache": true,
},
});
All imports
import {
ShopifyAPI,
ShopifyApp,
ShopifyAppOptions,
UserTokenFunc,
WebHook,
WebHookCall,
WebHookFunc,
} from "https://deno.land/x/shopify_deno/mod.ts";
import { req, res, Server } from "https://deno.land/x/faster/mod.ts";
This library has some more abstracted methods for some applications I created, explore the ShopifyAPI class (may be useful for you). Some examples are: “includeScripts” and “searchTags”.
About
Author: Henrique Emanoel Viana, a Brazilian computer scientist, enthusiast of web technologies, cel: +55 (41) 99999-4664. URL: https://sites.google.com/view/henriqueviana
Improvements and suggestions are welcome!