Instances of this class can make requests to the Shopify Admin REST API.
// Requests to /my-endpoint must be made with authenticatedFetch for embedded apps
app.get('/my-endpoint', async (req, res) => {
const sessionId = await shopify.session.getCurrentId({
isOnline: true,
rawRequest: req,
rawResponse: res,
});
// 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.Rest({
session,
apiVersion: ApiVersion.January23,
});
});
Receives an object containing:
Session
| ❗ required
The Shopify Session containing an access token to the API.
ApiVersion
This will override the default API version. Any requests made by this client will reach this version instead.
Sends a GET request to the Admin API.
const getResponse = await client.get({
path: 'products',
});
console.log(getResponse.headers, getResponse.body);
Note: If using TypeScript, you can also pass in a type argument to cast the response body:
interface MyResponseBodyType {
products: {
/* ... */
};
}
const response = await client.get<MyResponseBodyType>({
path: 'products',
});
// response.body will be of type MyResponseBodyType
console.log(response.body.products);
string
| ❗ required
The requested API endpoint path. This can be one of two formats:
- The path starting after the
/admin/api/{version}/
prefix, such as'products'
, which executes/admin/api/{version}/products.json
, where{version}
is obtained from the library configuration (seeshopifyApi
. - The full path, such as
/admin/oauth/access_scopes.json
{[key: string]: string | number}
An optional query argument object to append to the request URL.
{[key: string]: string | number}
Add custom headers to the request.
DataType
| Defaults to DataType.JSON
The Content-Type
for the request (JSON
, GraphQL
, URLEncoded
).
number
| Defaults to 1
, must be >= 0
The maximum number of times to retry the request.
Promise<RequestResponse>
Returns an object containing:
{[key: string]: string | string[]}
The HTTP headers in the response from Shopify.
any
The HTTP body in the response from Shopify.
Sends a DELETE request to the Admin API.
const deleteResponse = await client.delete({
path: 'products/123456',
});
console.log(deleteResponse.headers, deleteResponse.body);
Takes the same parameters as the get
method.
Promise<RequestResponse>
Returns the same object as the get
method.
Sends a POST request to the Admin API.
const postResponse = await client.post({
path: 'products',
data: {
title: 'My product title',
},
});
console.log(postResponse.headers, postResponse.body);
Takes the same parameters as the get
method, with the addition of:
{[key: string]: unknown} | string
The request payload.
Promise<RequestResponse>
Returns the same object as the get
method.
Sends a PUT request to the Admin API.
const putResponse = await client.put({
path: 'products/123456',
data: {
title: 'My product title',
},
});
console.log(putResponse.headers, putResponse.body);
Takes the same parameters as the post
method.
Promise<RequestResponse>
Returns the same object as the get
method.