This is Ogasawara, CTO of Tsun Inc.!
This time, we have summarized know-how for easily running the Shopify API with Postman.
It is not limited to the Shopify API, but running and checking an API from the console every time is surprisingly time-consuming.
In such cases, you can use an app called Postman to organize environment information and other settings so that you can execute and check requests quickly!
What is Postman?
Postman is a collaboration tool specialized for developing and testing APIs.
Postman is a collaboration platform for developing and testing APIs.
With Postman, you can check the response by setting a token and other details, then sending a request to the API endpoint.
Current Postman includes features such as HTTP, REST, SOAP, and GraphQL request testing, API design, API documentation, API discovery, Mock Server, Monitors, Workspaces, and more.
By using Postman, API development can be simplified and work efficiency can be improved.
Prerequisites
Target audience
- People who have used the Shopify Admin API
Environment
- Postman for Mac Version 10.11.1
Installing Postman
Follow the instructions at https://www.postman.com/ or https://circleci.com/ja/blog/testing-an-api-with-postman/ to sign up and install Postman.
Query the Shopify Admin API with Postman
Once installed, open Postman and start by running a simple query of the Shopify Admin API.
In order to execute Shopify Admin API queries, you need an API token, so first generate one.
Generate a token for the Shopify Admin API
Open Shopify admin for your store (preferably the development store).
From Settings > Apps and sales channels, click Develop apps.
Click Create an app.
I think the app name should be easy to understand, such as "Postman". (Assuming it is only used with Postman)
Once the app is created, set the access scopes from the settings on the app screen. (Keep access scopes to a minimum)
From API credentials, get the Admin API access token. (You can only obtain it once, so we recommend storing it in a safe place such as 1Password)
Now you're ready to run your query.
Execute GraphQL queries with Postman
Open Postman.
From Collections > Create new collection, create a new collection. (Enter an appropriate name)
You can now add new requests to the collection, so click Add request to add a request.
Change GET to POST.
Set the URL to https://{Your Shopify Domain}.myshopify.com/admin/api/2023-01/graphql.json.
Set Type on the Authorization tab to API Key.
Enter "X-Shopify-Access-Token" for Key, the token obtained above for Value, and set Add to to Header.
Write the following query in the Body tab.
query {
shop {
name
currencyCode
checkoutApiSupported
taxesIncluded
}
}
If you execute Send and get the shop data, it is a success!
Manage requests using variables
As the number of requests increases in the future, it will become a hassle to configure the above settings every time you create a new request.
Therefore, we will use Postman's variable function to make management easier.
First, create variables for the API version and Shopify domain.
Open the Variables tab from the collection settings.
Add variables called API_VERSION and SHOP. Set any value for each variable.
Change the URL of the previous request to one that uses variables.
It will look like this: https://{{SHOP}}.myshopify.com/admin/api/{{API_VERSION}}/graphql.json
Execute Send and confirm that you can get the same data as before.
Now, when you want to try a different version of the API, you can easily test it by changing variable values without having to rewrite all requests one by one!
Manage multiple stores separately
Updated in July 2023:
Instead of store management using collections, we have changed to switching using Postman's Environments function.
Please refer to this article as it is very easy to understand how to use Environments.
https://qiita.com/yokawasa/items/05913df60aea07395903
If you're a Shopify partner or have a development store and a production store, we recommend managing multiple stores separately.
By making the collection a store unit, you can use the variable mentioned earlier to standardize the URL of requests under the collection.
You can also share Authorization within a collection.
First, open the Authorization tab from the collection settings.
Set Type to API Key, and enter the same Key and Value as before.
Open the Authorization tab for each request.
Set Type to Inherit auth from parent.
This will reference the collection's Authorization, eliminating the need to rewrite all requests when changing the API Key.
Manage GraphQL API and REST API separately
There are two types of Shopify Admin API: GraphQL API and REST API.
It's a matter of preference, but you can also create a GraphQL folder and a REST folder under the collection and manage each API request separately.
Summary
This time, we introduced how to manage the Shopify Admin API using Postman.
Postman also has the ability to automatically fetch GraphQL schemas.
When writing GraphQL queries, auto-completion works, making it much easier to write queries.
Everyone, please use Postman and enjoy a comfortable Shopify Admin API workflow!