# Admin API

Contains objects used to interact with the Admin API.

This object is returned as part of different contexts, such as [`admin`](/docs/api/shopify-app-remix/authenticate/admin), [`unauthenticated.admin`](/docs/api/shopify-app-remix/unauthenticated/unauthenticated-admin), and [`webhook`](/docs/api/shopify-app-remix/authenticate/webhook).


## admin
Provides utilities that apps can use to make requests to the Admin API.

### AdminApiContext


### rest

value: `RestClientWithResources<Resources>`

Methods for interacting with the Shopify Admin REST API

There are methods for interacting with individual REST resources. You can also make `GET`, `POST`, `PUT` and `DELETE` requests should the REST resources not meet your needs.






### graphql

value: `GraphQLClient<AdminOperations>`

Methods for interacting with the Shopify Admin GraphQL API











### RestClientWithResources


RemixRestClient & {resources: Resources}

### RemixRestClient


### session

value: `Session`



### get

value: `(params: GetRequestParams) => Promise<Response>`

Performs a GET request on the given path.

### post

value: `(params: PostRequestParams) => Promise<Response>`

Performs a POST request on the given path.

### put

value: `(params: PostRequestParams) => Promise<Response>`

Performs a PUT request on the given path.

### delete

value: `(params: GetRequestParams) => Promise<Response>`

Performs a DELETE request on the given path.

### Session
Stores App information from logged in merchants so they can make authenticated requests to the Admin API.

### id

value: `string`

The unique identifier for the session.

### shop

value: `string`

The Shopify shop domain, such as `example.myshopify.com`.

### state

value: `string`

The state of the session. Used for the OAuth authentication code flow.

### isOnline

value: `boolean`

Whether the access token in the session is online or offline.

### scope

value: `string`

The desired scopes for the access token, at the time the session was created.

### expires

value: `Date`

The date the access token expires.

### accessToken

value: `string`

The access token for the session.

### onlineAccessInfo

value: `OnlineAccessInfo`

Information on the user for the session. Only present for online sessions.

### isActive

value: `(scopes: string | string[] | AuthScopes) => boolean`

Whether the session is active. Active sessions have an access token that is not expired, and has the given scopes.

### isScopeChanged

value: `(scopes: string | string[] | AuthScopes) => boolean`

Whether the access token has the given scopes.

### isExpired

value: `(withinMillisecondsOfExpiry?: number) => boolean`

Whether the access token is expired.

### toObject

value: `() => SessionParams`

Converts an object with data into a Session.

### equals

value: `(other: Session) => boolean`

Checks whether the given session is equal to this session.

### toPropertyArray

value: `(returnUserData?: boolean) => [string, string | number | boolean][]`

Converts the session into an array of key-value pairs.

### OnlineAccessInfo


### expires_in

value: `number`

How long the access token is valid for, in seconds.

### associated_user_scope

value: `string`

The effective set of scopes for the session.

### associated_user

value: `OnlineAccessUser`

The user associated with the access token.

### OnlineAccessUser


### id

value: `number`

The user's ID.

### first_name

value: `string`

The user's first name.

### last_name

value: `string`

The user's last name.

### email

value: `string`

The user's email address.

### email_verified

value: `boolean`

Whether the user has verified their email address.

### account_owner

value: `boolean`

Whether the user is the account owner.

### locale

value: `string`

The user's locale.

### collaborator

value: `boolean`

Whether the user is a collaborator.

### AuthScopes
A class that represents a set of access token scopes.

### has

value: `(scope: string | string[] | AuthScopes) => boolean`

Checks whether the current set of scopes includes the given one.

### equals

value: `(otherScopes: string | string[] | AuthScopes) => boolean`

Checks whether the current set of scopes equals the given one.

### toString

value: `() => string`

Returns a comma-separated string with the current set of scopes.

### toArray

value: `() => any[]`

Returns an array with the current set of scopes.

### SessionParams


### [key: string]

value: `any`



### id

value: `string`

The unique identifier for the session.

### shop

value: `string`

The Shopify shop domain.

### state

value: `string`

The state of the session. Used for the OAuth authentication code flow.

### isOnline

value: `boolean`

Whether the access token in the session is online or offline.

### scope

value: `string`

The scopes for the access token.

### expires

value: `Date`

The date the access token expires.

### accessToken

value: `string`

The access token for the session.

### onlineAccessInfo

value: `OnlineAccessInfo | StoredOnlineAccessInfo`

Information on the user for the session. Only present for online sessions.

### StoredOnlineAccessInfo


Omit<OnlineAccessInfo, 'associated_user'> & {
  associated_user: Partial<OnlineAccessUser>;
}

### GetRequestParams


### path

value: `string`

The path to the resource, relative to the API version root.

### type

value: `DataType`

The type of data expected in the response.

### data

value: `string | Record<string, any>`

The request body.

### query

value: `SearchParams`

Query parameters to be sent with the request.

### extraHeaders

value: `HeaderParams`

Additional headers to be sent with the request.

### tries

value: `number`

The maximum number of times the request can be made if it fails with a throttling or server error.

### DataType


### JSON

value: `application/json`



### GraphQL

value: `application/graphql`



### URLEncoded

value: `application/x-www-form-urlencoded`



### HeaderParams
Headers to be sent with the request.

Record<string, string | number | string[]>

### PostRequestParams


GetRequestParams & {
  data: Record<string, any> | string;
}

### GraphQLClient


#### Returns: interface Promise<T> {
    /**
     * Attaches callbacks for the resolution and/or rejection of the Promise.
     * @param onfulfilled The callback to execute when the Promise is resolved.
     * @param onrejected The callback to execute when the Promise is rejected.
     * @returns A Promise for the completion of which ever callback is executed.
     */
    then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;

    /**
     * Attaches a callback for only the rejection of the Promise.
     * @param onrejected The callback to execute when the Promise is rejected.
     * @returns A Promise for the completion of the callback.
     */
    catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
}, interface Promise<T> {}, Promise: PromiseConstructor, interface Promise<T> {
    readonly [Symbol.toStringTag]: string;
}, interface Promise<T> {
    /**
     * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The
     * resolved value cannot be modified from the callback.
     * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
     * @returns A Promise for the completion of the callback.
     */
    finally(onfinally?: (() => void) | undefined | null): Promise<T>;
}


#### Params:
- query: Operation extends keyof Operations
- options: GraphQLQueryOptions<Operation, Operations>

export type GraphQLClient<Operations extends AllOperations> = <
  Operation extends keyof Operations,
>(
  query: Operation,
  options?: GraphQLQueryOptions<Operation, Operations>,
) => Promise<GraphQLResponse<Operation, Operations>>;

### GraphQLQueryOptions


### variables

value: `ApiClientRequestOptions<Operation, Operations>["variables"]`

The variables to pass to the operation.

### apiVersion

value: `ApiVersion`

The version of the API to use for the request.

### headers

value: `Record<string, any>`

Additional headers to include in the request.

### tries

value: `number`

The total number of times to try the request if it fails.

### ApiVersion


### October22

value: `2022-10`



### January23

value: `2023-01`



### April23

value: `2023-04`



### July23

value: `2023-07`



### October23

value: `2023-10`



### January24

value: `2024-01`



### April24

value: `2024-04`



### Unstable

value: `unstable`



## Related
- [Authenticated context](/docs/api/shopify-app-remix/authenticate/admin)
- [Unauthenticated context](/docs/api/shopify-app-remix/unauthenticated/unauthenticated-admin)

## Examples
Contains objects used to interact with the Admin API.

This object is returned as part of different contexts, such as [`admin`](/docs/api/shopify-app-remix/authenticate/admin), [`unauthenticated.admin`](/docs/api/shopify-app-remix/unauthenticated/unauthenticated-admin), and [`webhook`](/docs/api/shopify-app-remix/authenticate/webhook).
### rest

### Using REST resources

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

export const loader = async ({ request }: LoaderFunctionArgs) => {
  const {
    admin,
    session,
  } = await authenticate.admin(request);

  return json(
    admin.rest.resources.Order.count({ session }),
  );
};
```

```typescript
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;
```


### Performing a GET request to the REST API

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

export const loader = async ({ request }: LoaderFunctionArgs) => {
  const {
    admin,
    session,
  } = await authenticate.admin(request);

  const response = await admin.rest.get({
    path: "/customers/count.json",
  });
  const customers = await response.json();

  return json({ customers });
};
```

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

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


### Performing a POST request to the REST API

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

export const loader = async ({ request }: LoaderFunctionArgs) => {
  const {
    admin,
    session,
  } = await authenticate.admin(request);

  const response = admin.rest.post({
    path: "customers/7392136888625/send_invite.json",
    body: {
      customer_invite: {
        to: "new_test_email@shopify.com",
        from: "j.limited@example.com",
        bcc: ["j.limited@example.com"],
        subject: "Welcome to my new shop",
        custom_message: "My awesome new store",
      },
    },
  });

  const customerInvite = await response.json();
  return json({ customerInvite });
};
```

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

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


### graphql

### Querying the GraphQL API

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

export const action = async ({ request }: ActionFunctionArgs) => {
  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({
    productId: productData.data?.productCreate?.product?.id,
  });
}
```

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

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


### Handling GraphQL errors

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

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

  try {
    const response = await admin.graphql(
      `#graphql
      query incorrectQuery {
        products(first: 10) {
          nodes {
            not_a_field
          }
        }
      }`,
    );

    return json({ data: await response.json() });
  } catch (error) {
    if (error instanceof GraphqlQueryError) {
      // error.body.errors:
      // { graphQLErrors: [
      //   { message: "Field 'not_a_field' doesn't exist on type 'Product'" }
      // ] }
      return json({ errors: error.body?.errors }, { status: 500 });
    }
    return json({ message: "An error occurred" }, { status: 500 });
  }
}
```

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

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