Admin API
The Admin API is a private API that allows you to manage your Shopify store's data, such as orders, customers, products, and inventory. It is designed to be used in server-side applications only, as it requires a private access token that should never be exposed to the client. To use the Admin API, you need to obtain a private access token with the appropriate scopes.
Usage
Once configured, you can use the Admin API in your Nuxt application via the useAdmin
composable.
This composable can only be used on the server-side (in server routes, middleware, or server-only composables).
Server-side
useAdmin
When setting the accessToken
for the admin client in the module configuration, you can use the useAdmin
composable
in your server-side code. For example, in a server route:
export default defineEventHandler(async (event) => {
const admin = useAdmin()
return admin.request(`#graphql
query GetOrders($first: Int!) {
orders(first: $first) {
edges {
node {
id
name
totalPrice
createdAt
}
}
}
}
`, {
variables: {
first: 10,
}
})
})
You can also wrap the useAdmin
composable in another composable to build an abstracted data fetching method:
export const useOrder = (id: string) => {
const admin = useAdmin()
return admin.request(`#graphql
query GetOrder($id: ID!) {
order(id: $id) {
id
name
totalPrice
lineItems(first: 10) {
edges {
node {
title
quantity
}
}
}
}
}
`, {
variables: {
id,
}
})
}
Using validation
Using Nitro's built-in input validation, you can match the variables of your GraphQL queries before sending them to the API.
For this example we'll use the Zod library, but you can use any validation library you like.
First, install the validation library:
npm install zod
Then, import it and create a schema:
import { z } from 'zod'
const schema = z.object({
first: z.preprocess(v => Number(v), z.number().min(1).max(250)),
status: z.enum(['OPEN', 'CLOSED', 'CANCELLED']).optional(),
})
Next, we can use Nitro's built-in getValidatedQuery
utility to validate the query variables:
import { z } from 'zod'
const schema = z.object({
first: z.preprocess(v => Number(v), z.number().min(1).max(250)),
status: z.enum(['OPEN', 'CLOSED', 'CANCELLED']).optional(),
})
export default defineEventHandler(async (event) => {
const admin = useAdmin()
const variables = await getValidatedQuery(event, schema.parse)
const query = `#graphql
query FetchOrders($first: Int!, $status: OrderStatus) {
orders(first: $first, query: $status) {
edges {
node {
id
name
totalPrice
financialStatus
fulfillmentStatus
}
}
}
}
`
return admin.request(query, { variables })
})
Now we can call the API at /api/orders
with the following variables:
const { data: orders } = await useFetch('/api/orders', {
query: {
first: 50,
status: 'OPEN',
},
})
Now the request will fail before reaching Shopify if the variables don't match the schema.