shopify-api-js
repository·main·Indexed 21 days ago
https://github.com/shopify/shopify-api-jsA collection of JavaScript API client libraries and utilities for interacting with Shopify's Admin, Storefront, and GraphQL APIs. Includes the @shopify/admin-api-client for GraphQL and REST interfaces, @shopify/graphql-client, and @shopify/api-codegen-preset for generating TypeScript types for GraphQL operations.
What's inside shopify-api-js
- The Admin API Client is a lightweight and minimally opinionated library designed for developers who need to interact with Shopify's Admin API. It provides support for both GraphQL and REST interfaces.
Overview of @shopify/shopify-api packages
mainThe
@shopify/shopify-api-jsmono-repo provides a collection of JavaScript API client libraries and utilities for interacting with Shopify services. The specific packages available are:@shopify/shopify-api: A server-side library for managing OAuth (online/offline access tokens), making Admin API (REST/GraphQL) and Storefront API (GraphQL) requests, and registering/processing webhooks.@shopify/storefront-api-client: A client for interacting with the GraphQL Storefront API. It can be used on both the client and the server.@shopify/admin-api-client: A server-side library for interacting with GraphQL and REST Admin APIs.@shopify/graphql-client: A generic client for interacting with any of Shopify's GraphQL APIs.@shopify/api-codegen-preset: A utility that enables JavaScript/TypeScript apps to use the#graphqltag to parse queries usinggraphql-codegen.
Explore the @shopify/shopify-api library reference
mainThe
@shopify/shopify-apilibrary is organized into several main components that handle different aspects of interacting with Shopify. Use the following top-level objects to manage your app's integration:config: Options used to initialize the library.auth: Functions for authenticating with Shopify APIs.clients: Clients used to make requests to Shopify APIs (e.g., GraphQL or REST).session: Functions for managing Shopify sessions.webhooks: Functions to configure and handle Shopify webhooks.billing: Functions to enable app billing for merchants.utils: General utility functions for app development.rest: Object-oriented representations of the Admin REST API resources.
Use the shopify.billing object to manage merchant payments
mainThe
shopify.billingobject provides methods to manage billing charges with Shopify. These methods are based on the plans defined in your app'sbillingconfiguration.Important Requirements:
- This package uses the GraphQL Admin API to request or check payments.
- Your app must complete the OAuth process before it can successfully charge merchants.
Available properties on the
shopify.billingobject:check: Verifies if the current shop has already paid for specific plans.request: Initiates a new payment request for a specific payment plan.cancel: Cancels an existing subscription plan using its subscription ID.subscriptions: Retrieves a list of all subscription plans the current shop has paid for.
Use shopify.utils for common helper functions
mainTheshopify.utilsobject provides a collection of generic helper functions designed to simplify common tasks in Shopify app development, such as input sanitization, security validation, and version compatibility checks.Use shopify.clients to interact with Shopify APIs
mainThe
shopify.clientsobject provides specialized classes for interacting with different Shopify API surfaces. Depending on your application's needs, you can use these clients to perform RESTful operations, execute GraphQL queries, or interact with the Storefront API.import { shopifyApi } from '@shopify/shopify-api'; // Accessing clients via the shopifyApi instance const restClient = shopifyApi.clients.Rest; const graphqlClient = shopifyApi.clients.Graphql; const storefrontClient = shopifyApi.clients.Storefront; const graphqlProxy = shopifyApi.clients.graphqlProxy;Configure, register, and process webhooks with shopify.webhooks
mainThe
shopify.webhooksobject provides a complete suite of functions to manage the lifecycle of Shopify webhooks within your application. You can use it to:- Configure handlers: Use
addHandlersto map specific webhook topics to your application's logic. - Register webhooks: Use
registerto communicate with Shopify and ensure the configured topics are actually subscribed to in your store. - Process incoming requests: Use
processto validate the authenticity of a webhook request received from Shopify and execute the associated handlers. - Inspect registry: Use
getTopicsAddedto see which topics are currently in your local registry, orgetHandlersto inspect the handlers configured for a specific topic.
- Configure handlers: Use
Choose between Token Exchange and Authorization Code Grant Flow
mainWhen implementing OAuth, choose the flow based on your app type:
Token Exchange (Recommended for embedded apps):
- Uses a user's session token to retrieve an [access token].
- Faster and prevents flickering because it doesn't require redirects.
- If using Shopify managed installation, Shopify automatically handles access scope changes.
Authorization Code Grant Flow (Suitable for non-embedded apps):
- The app manages installations and access scope changes manually.
- Requires creating two endpoints to handle redirects.
Important: Webhook body parsing and `rawBody`
mainIn current versions of the library,
shopify.webhooks.process()expects the body content to be passed explicitly as a string via therawBodyparameter. It no longer reads the request body directly from the stream.Express Configuration
If you use
express.json()globally in your app, you must ensure that your webhook endpoint usesexpress.text({type: '*/*'})middleware. This ensuresreq.bodyis a string, which is required for successful validation and processing:await shopify.webhooks.process({ rawBody: req.body, // must be a string rawRequest: req, rawResponse: res, });Understand the Session object structure
mainA
Sessionobject contains the authentication and identity data required to make API calls. WhenisOnlineistrue, theexpiresandonlineAccessInfoproperties will be populated.Property Type Mandatory? Description idstringyes shopstringyes statestringyes isOnlinebooleanyes scopestringno expiresDateno accessTokenstringno onlineAccessInfoOnlineAccessInfono When to check for merchant payment
mainBecause billing requires an active session, your app must be installed before it can request payment. You can use the
checkmethod to gate access to your app or specific endpoints.Recommended check points:
- After OAuth completes: Use the session returned from
shopify.auth.callbackto ensure billing is part of the authentication flow. - When validating frontend requests: Use
shopify.session.getCurrentIdto run checks on incoming requests.
Important: Merchants can cancel subscriptions after installation. It is highly recommended to use billing webhooks to revoke access when a merchant cancels or declines payment.
- After OAuth completes: Use the session returned from
Authenticate Shopify Flow extension requests with shopify.flow
mainTheshopify.flowobject provides utilities to authenticate incoming requests from Shopify Flow extensions. Use thevalidatemethod to verify that a request is a valid Shopify Flow extension request, ensuring the authenticity of the communication from Shopify.