Example app that connects to third-party Shipping API and works with Saleor.
This app is an example of how to integrate with the Saleor shipping API. The diagram below illustrates the flow of requests:
sequenceDiagram
Actor C as Customer
participant SF as Storefront
participant SALEOR as Saleor API
participant SSA as Shipstation Shipping App
participant SS as Shipstation
C ->>+ SF: Add products to cart
SF ->>+ SALEOR: checkoutCreate/checkoutUpdate
SALEOR ->>- SF: Checkout
SF ->>- C: Checkout
C ->>+ SF: Add shipping address
SF ->>+ SALEOR: checkoutCreate/checkoutUpdate
SALEOR ->>- SF: Checkout
C ->>+ SF: Show me shipping options
SF ->>+ SALEOR: checkout.shippingMethods
SALEOR ->>+ SSA: sync webhook <br> SHIPPING_LIST_METHODS_FOR_CHECKOUT
SSA ->> SSA: calculate weight
SSA ->> SSA: determine the package size
SSA ->>+ SS: fetch getRates
SS ->>- SSA: available rates
SSA ->> SSA: format rates to Saleor response type
SSA ->>- SALEOR: list of shippingMethods
SALEOR ->>- SF: list of shippingMethods
SF ->>- C: Shipping options are presented
C ->>+ SF: Choose a shipping method
SF ->>+ SALEOR: checkoutDeliveryMethodUpdate
SALEOR ->>- SF: checkout updated
SF ->> C: Checkout
The weight is the sum of of all of the items weights (CheckoutLine.variant.weight
) and supports grams, ounces and pounds.
Example code is assuming 3 types of available packages:
- envelope
- small box
- big box
The types and their sizes are defined in /src/modules/shipstation/types.ts
.
When shipping methods are requested, the App checks the value of Product attribute with slug "package-size", and chooses which package will fit whole checkout. Eg.:
- Checkout contains 2 "envelope" sized items. Since it's below limit of the "envelope" package (5pcs), "envelope" will be used.
- Checkout contains 2 "envelope" and 1 "big box". The only package which can fit all the products will be "big box"
Code can be found in file /src/modules/shipstation/saleor-to-shipstation.ts
.
You can use the third-party API to request shipping methods for a given checkout session. Since a checkout can be created with or without a shipping address, you may need additional logic for these two scenarios. For more information, refer to the documentation.
Placeholder webhook which can be used to notify service responsible for preparing shipping labels. Docs
Standard flow for storefront:
checkoutCreate
. Creates checkout with shipping address.checkoutDeliveryMethodUpdate
. Select delivery method.checkoutComplete
. Completes checkout and creates an order.
- Run app in development mode
- Send requests via Bruno
Before you start, make sure you have installed:
- Install the dependencies by running:
pnpm install
- Start the local server with:
pnpm dev
-
Expose local environment using tunnel: Use tunneling tools like localtunnel or ngrok.
-
Create .env file with URL of the exposed app and required API keys (reference in
.env.example
) -
Install the application in your dashboard:
If you use Saleor Cloud or your local server is exposed, you can install your app by following this link:
[YOUR_SALEOR_DASHBOARD_URL]/apps/install?manifestUrl=[YOUR_APP_TUNNEL_MANIFEST_URL]
This template host manifest at /api/manifest
You can also install application using GQL or command line. Follow the guide how to install your app to learn more.
Commands build
and dev
would generate schema and typed functions using Saleor's GraphQL endpoint. Commit the generated
folder to your repo as they are necessary for queries and keeping track of the schema changes.
Learn more about GraphQL code generation.
During the registration process, Saleor API passes the auth token to the app. With this token App can query Saleor API with privileged access (depending on requested permissions during the installation). To store this data, app-template use a different APL interfaces.
The choice of the APL is made using the APL
environment variable. If the value is not set, FileAPL is used. Available choices:
file
: no additional setup is required. Good choice for local development. It can't be used for multi tenant-apps or be deployed (not intended for production)upstash
: use Upstash Redis as storage method. Free account required. It can be used for development and production and supports multi-tenancy. RequiresUPSTASH_URL
andUPSTASH_TOKEN
environment variables to be set
If you want to use your own database, you can implement your own APL. Check the documentation to read more.
If you have your application up and running inside Saleor Dashboard now its time to run GraphQL mutations. To get started you need:
- Create
.env
insidebruno
folder based on.env.example
- Fill the needed env variables
- Open Bruno and send request in sequence they are defined to make full checkout flow
- carriers list (eg. stamps_com, ups) is hardcoded in the envs, and at the moment only one is supported
- all saleor variants have to follow the same weight unit
- the app assumes the shipping rates returned from the Shipstation are using USD as currency - channel configuration should follow this format
- considering SLIM shipstation rate limits (40 req/s) mitigation measures should be taken
- aggressive caching (based on postalCodes and checkout weight)
- contacting Shipstation to modify the limits