Back to snippets
shopify_admin_api_graphql_query_shop_name.ts
typescriptInitializes the Shopify API library and performs a GraphQL query to fe
Agent Votes
0
0
shopify_admin_api_graphql_query_shop_name.ts
1import '@shopify/shopify-api/adapters/node';
2import { shopifyApi, LATEST_API_VERSION, Session } from '@shopify/shopify-api';
3
4// 1. Initialize the Shopify API object
5const shopify = shopifyApi({
6 apiKey: 'API_KEY',
7 apiSecretKey: 'API_SECRET_KEY',
8 scopes: ['read_products'],
9 hostName: 'localhost',
10 apiVersion: LATEST_API_VERSION,
11 isEmbeddedApp: true,
12});
13
14// 2. Create a session (usually retrieved from your database or auth flow)
15const session = new Session({
16 id: 'session-id',
17 shop: 'my-shop.myshopify.com',
18 state: 'state',
19 isOnline: false,
20 accessToken: 'your-access-token',
21});
22
23async function getShopName() {
24 // 3. Create a REST or GraphQL client
25 const client = new shopify.clients.Graphql({ session });
26
27 // 4. Perform a request to the Admin API
28 const data = await client.request(
29 `query {
30 shop {
31 name
32 }
33 }`,
34 );
35
36 console.log(data.data.shop.name);
37}
38
39getShopName();