0

Updates an existing gift card

by
Published Nov 8, 2023

Updates an existing gift card. The gift card's balance can't be changed via the API. You can change only the expiry date, note, and template suffix.

Script shopify Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 416 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Updates an existing gift card
7
 * Updates an existing gift card. The gift card's balance can't be changed via the API. You can change only the expiry date, note, and template suffix.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  gift_card_id: string,
13
  body: {
14
    gift_card?: { id?: number; note?: string; [k: string]: unknown };
15
    [k: string]: unknown;
16
  }
17
) {
18
  const url = new URL(
19
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/gift_cards/${gift_card_id}.json`
20
  );
21

22
  const response = await fetch(url, {
23
    method: "PUT",
24
    headers: {
25
      "Content-Type": "application/json",
26
      "X-Shopify-Access-Token": auth.token,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36