Skip to main content

Admin

Contains methods for authenticating and interacting with the Admin API.

This function can handle requests for apps embedded in the Admin, Admin extensions, or non-embedded apps.

Authenticates requests coming from the Shopify admin.

Note

The shape of the returned object changes depending on the distribution config. Merchant custom apps (AppDistribution.ShopifyAdmin) do not return session tokens or redirect functionality. All other distributions return a context with session tokens and redirect functionality.

Caution

For merchant custom apps (AppDistribution.ShopifyAdmin), authenticate.admin does not authenticate the request. These apps can't be embedded in the Shopify admin, so requests don't carry a session token. The function returns an Admin API context that uses your app's adminApiAccessToken for any request it receives. Your app must authenticate its own users before it calls authenticate.admin, for example with your identity provider, a private network, or HTTP authentication.

Anchor to request
request
Request
required

Promise<<Config>>
Examples

/app/routes/**.ts

import {type ActionFunctionArgs, data} from 'react-router';
import {GraphqlQueryError} from '@shopify/shopify-api';

import {authenticate} from '../shopify.server';

export const action = async ({request}: ActionFunctionArgs) => {
const {admin, redirect} = await authenticate.admin(request);

try {
await admin.graphql(
`#graphql
mutation updateProductTitle($input: ProductInput!) {
productUpdate(input: $input) {
product {
id
}
}
}`,
{
variables: {
input: {id: '123', title: 'New title'},
},
},
);

return redirect('/app/product-updated');
} catch (error) {
if (error instanceof GraphqlQueryError) {
return data({errors: error.body?.errors}, {status: 500});
}

return new Response('Failed to update product title', {status: 500});
}
};


Was this page helpful?