Skip to main content

Admin
object

Contains functions 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.

The shape of the returned object changes depending on the isEmbeddedApp config.

Request
required

Promise<<Config, Resources>>
Was this section helpful?

Get user-specific data using the sessionToken object.

Was this section helpful?

Using the decoded session token

import { shopifyApp } from "@shopify/shopify-app-remix/server";

const shopify = shopifyApp({
// ...etc
useOnlineTokens: true,
});
export default shopify;
export const authenticate = shopify.authenticate;

Use the redirect helper to safely redirect between pages.

Anchor to example-redirecting-outside-of-shopify-adminRedirecting outside of Shopify admin

Pass in a target option of _top or _parent to go to an external URL.

Was this section helpful?

Redirecting to an app route

/app/routes/admin/my-route.ts

import { LoaderArgs, json } from "@remix-run/node";
import { authenticate } from "../shopify.server";

export const loader = async ({ request }: LoaderArgs) => {
const { session, redirect } = await authenticate.admin(request);
return redirect("/");
};

Get your app's shop-specific data using an offline session.

Get your app's user-specific data using an online session.

Was this section helpful?

Using offline sessions

import { shopifyApp } from "@shopify/shopify-app-remix/server";

const shopify = shopifyApp({
// ...etc
});
export default shopify;
export const authenticate = shopify.authenticate;

Anchor to example-setting-cors-headers-for-a-admin-requestSetting CORS headers for a admin request

Use the cors helper to ensure your app can respond to requests from admin extensions.

Was this section helpful?

Setting CORS headers for a admin request

/app/routes/admin/my-route.ts

import { LoaderArgs, json } from "@remix-run/node";
import { authenticate } from "../shopify.server";
import { getMyAppData } from "~/db/model.server";

export const loader = async ({ request }: LoaderArgs) => {
const { session, cors } = await authenticate.admin(request);
return cors(json(await getMyAppData({user: session.onlineAccessInfo!.id})));
};

Getting the number of orders in a store using REST resources.

Anchor to example-performing-a-get-request-to-the-rest-apiPerforming a GET request to the REST API

Use admin.rest.<method> to make custom requests to the API.

Was this section helpful?

Using REST resources

import { shopifyApp } from "@shopify/shopify-app-remix/server";
import { restResources } from "@shopify/shopify-api/rest/admin/2023-07";

const shopify = shopifyApp({
restResources,
// ...etc
});
export default shopify;
export const authenticate = shopify.authenticate;

Use admin.graphql to make query / mutation requests.

Was this section helpful?

Querying the GraphQL API

Example

import { ActionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";

export async function action({ request }: ActionArgs) {
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
`#graphql
mutation populateProduct($input: ProductInput!) {
productCreate(input: $input) {
product {
id
}
}
}`,
{ variables: { input: { title: "Product Name" } } }
);

const productData = await response.json();
return json({ data: productData.data });
}

Call billing.request in the onFailure callback to immediately request payment.

Redirect to a different page in the onFailure callback, where the merchant can select a billing plan.

Was this section helpful?

Requesting billing right away

import { LoaderArgs } from "@remix-run/node";
import { authenticate, MONTHLY_PLAN } from "../shopify.server";

export const loader = async ({ request }: LoaderArgs) => {
const { billing } = await authenticate.admin(request);
await billing.require({
plans: [MONTHLY_PLAN],
isTest: true,
onFailure: async () => billing.request({ plan: MONTHLY_PLAN }),
});

// App logic
};

Change where the merchant is returned to after approving the purchase using the returnUrl option.

Was this section helpful?

Using a custom return URL

import { LoaderArgs } from "@remix-run/node";
import { authenticate, MONTHLY_PLAN } from "../shopify.server";

export const loader = async ({ request }: LoaderArgs) => {
const { billing } = await authenticate.admin(request);
await billing.require({
plans: [MONTHLY_PLAN],
onFailure: async () => billing.request({
plan: MONTHLY_PLAN,
isTest: true,
returnUrl: '/billing-complete',
}),
});

// App logic
};

Use the billing.cancel function to cancel an active subscription with the id returned from billing.require.

Was this section helpful?

Cancelling a subscription

import { LoaderArgs } from "@remix-run/node";
import { authenticate, MONTHLY_PLAN } from "../shopify.server";

export const loader = async ({ request }: LoaderArgs) => {
const { billing } = await authenticate.admin(request);
const billingCheck = await billing.require({
plans: [MONTHLY_PLAN],
onFailure: async () => billing.request({ plan: MONTHLY_PLAN }),
});

const subscription = billingCheck.appSubscriptions[0];
const cancelledSubscription = await billing.cancel({
subscriptionId: subscription.id,
isTest: true,
prorate: true,
});

// App logic
};