//native
/**
* Create Gift Card
* Issue a gift card with an initial value (currency_code defaults to the shop currency). Leave code empty to let Shopify generate one; the full code is only returned now, as giftCardCode. Needs the write_gift_cards scope.
*/
export async function main(
auth: RT.Shopify,
initial_value: number,
currency_code: string | undefined,
code: string | undefined,
customer_id: string | undefined,
note: string | undefined,
expires_on: string | undefined,
template_suffix: string | undefined
) {
const endpoint = `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`
const headers = {
"X-Shopify-Access-Token": auth.token,
"Content-Type": "application/json",
Accept: "application/json",
}
let currencyCode = currency_code
if (!currencyCode) {
const shopResponse = await fetch(endpoint, {
method: "POST",
headers,
body: JSON.stringify({ query: `query { shop { currencyCode } }` }),
})
if (!shopResponse.ok) {
throw new Error(`${shopResponse.status} ${await shopResponse.text()}`)
}
const shop = await shopResponse.json()
if (shop.errors) {
throw new Error(JSON.stringify(shop.errors))
}
currencyCode = shop.data.shop.currencyCode
}
const response = await fetch(endpoint, {
method: "POST",
headers,
body: JSON.stringify({
query: `mutation ($input: GiftCardCreateInput!) { giftCardCreate(input: $input) { giftCard { id maskedCode lastCharacters enabled expiresOn note templateSuffix createdAt initialValue { amount currencyCode } balance { amount currencyCode } customer { id displayName } } giftCardCode userErrors { field message } } }`,
variables: {
input: {
initialAmount: { amount: String(initial_value), currencyCode },
code: code || undefined,
customerId: customer_id
? customer_id.startsWith("gid://")
? customer_id
: `gid://shopify/Customer/${customer_id}`
: undefined,
note: note || undefined,
expiresOn: expires_on || undefined,
templateSuffix: template_suffix || undefined,
},
},
}),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { data, errors } = await response.json()
if (errors) {
throw new Error(JSON.stringify(errors))
}
if (data.giftCardCreate.userErrors.length) {
throw new Error(JSON.stringify(data.giftCardCreate.userErrors))
}
return data.giftCardCreate
}
Submitted by hugo989 3 hours ago