# checkoutDiscountCodeApplyV2 - storefront - MUTATION Version: 2024-04 ## Description Applies a discount to an existing checkout using a discount code. ### Access Scopes `unauthenticated_write_checkouts` access scope. ## Arguments * [checkoutId](/docs/api/storefront/2024-04/scalars/ID): ID! - The ID of the checkout. * [discountCode](/docs/api/storefront/2024-04/scalars/String): String! - The discount code to apply to the checkout. ## Returns * [checkout](/docs/api/storefront/2024-04/objects/Checkout): Checkout The updated checkout object. * [checkoutUserErrors](/docs/api/storefront/2024-04/objects/CheckoutUserError): CheckoutUserError! The list of errors that occurred from executing the mutation. * [userErrors](/docs/api/storefront/2024-04/objects/UserError): UserError! The list of errors that occurred from executing the mutation. ## Examples ### Apply a discount code to a checkout Curl example: "curl -X POST \\\nhttps://your-development-store.myshopify.com/api/2024-04/graphql.json \\\n-H 'Content-Type: application/json' \\\n-H 'X-Shopify-Storefront-Access-Token: {storefront_access_token}' \\\n-d '{\n\"query\": \"mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) { checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) { checkout { discountApplications(first: 10) { edges { node { allocationMethod targetSelection targetType } } } } checkoutUserErrors { message code field } } }\",\n \"variables\": {\n \"checkoutId\": \"gid://shopify/Checkout/9234567890qwerty\",\n \"discountCode\": \"fixed_amount_off_code\"\n }\n}'\n" Node example: "const client = new shopify.clients.Storefront({\n domain: 'your-development-store.myshopify.com',\n storefrontAccessToken,\n});\nconst data = await client.query({\n data: {\n \"query\": `mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {\n checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {\n checkout {\n discountApplications(first: 10) {\n edges {\n node {\n allocationMethod\n targetSelection\n targetType\n }\n }\n }\n }\n checkoutUserErrors {\n message\n code\n field\n }\n }\n }`,\n \"variables\": {\n \"checkoutId\": \"gid://shopify/Checkout/9234567890qwerty\",\n \"discountCode\": \"fixed_amount_off_code\"\n },\n },\n});\n" Ruby example: null Remix example: "const { storefront } = await unauthenticated.storefront(\n 'your-development-store.myshopify.com'\n);\n\nconst response = await storefront.graphql(\n `#graphql\n mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {\n checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {\n checkout {\n discountApplications(first: 10) {\n edges {\n node {\n allocationMethod\n targetSelection\n targetType\n }\n }\n }\n }\n checkoutUserErrors {\n message\n code\n field\n }\n }\n }`,\n {\n variables: {\n \"checkoutId\": \"gid://shopify/Checkout/9234567890qwerty\",\n \"discountCode\": \"fixed_amount_off_code\"\n },\n },\n);\n\nconst data = await response.json();\n" Graphql query: "mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {\n checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {\n checkout {\n discountApplications(first: 10) {\n edges {\n node {\n allocationMethod\n targetSelection\n targetType\n }\n }\n }\n }\n checkoutUserErrors {\n message\n code\n field\n }\n }\n}" #### Graphql Input { "checkoutId": "gid://shopify/Checkout/9234567890qwerty", "discountCode": "fixed_amount_off_code" } #### Graphql Response { "data": { "checkoutDiscountCodeApplyV2": { "checkout": { "discountApplications": { "edges": [ { "node": { "allocationMethod": "ACROSS", "targetSelection": "ALL", "targetType": "LINE_ITEM" } } ] } }, "checkoutUserErrors": [] } } } ### Apply a discount code to a checkout that is currently being updated Curl example: "curl -X POST \\\nhttps://your-development-store.myshopify.com/api/2024-04/graphql.json \\\n-H 'Content-Type: application/json' \\\n-H 'X-Shopify-Storefront-Access-Token: {storefront_access_token}' \\\n-d '{\n\"query\": \"mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) { checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) { checkout { discountApplications(first: 10) { edges { node { allocationMethod targetSelection targetType } } } } checkoutUserErrors { message code field } } }\",\n \"variables\": {\n \"checkoutId\": \"gid://shopify/Checkout/9234567890qwerty\",\n \"discountCode\": \"fixed_amount_off_code\"\n }\n}'\n" Node example: "const client = new shopify.clients.Storefront({\n domain: 'your-development-store.myshopify.com',\n storefrontAccessToken,\n});\nconst data = await client.query({\n data: {\n \"query\": `mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {\n checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {\n checkout {\n discountApplications(first: 10) {\n edges {\n node {\n allocationMethod\n targetSelection\n targetType\n }\n }\n }\n }\n checkoutUserErrors {\n message\n code\n field\n }\n }\n }`,\n \"variables\": {\n \"checkoutId\": \"gid://shopify/Checkout/9234567890qwerty\",\n \"discountCode\": \"fixed_amount_off_code\"\n },\n },\n});\n" Ruby example: null Remix example: "const { storefront } = await unauthenticated.storefront(\n 'your-development-store.myshopify.com'\n);\n\nconst response = await storefront.graphql(\n `#graphql\n mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {\n checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {\n checkout {\n discountApplications(first: 10) {\n edges {\n node {\n allocationMethod\n targetSelection\n targetType\n }\n }\n }\n }\n checkoutUserErrors {\n message\n code\n field\n }\n }\n }`,\n {\n variables: {\n \"checkoutId\": \"gid://shopify/Checkout/9234567890qwerty\",\n \"discountCode\": \"fixed_amount_off_code\"\n },\n },\n);\n\nconst data = await response.json();\n" Graphql query: "mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {\n checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {\n checkout {\n discountApplications(first: 10) {\n edges {\n node {\n allocationMethod\n targetSelection\n targetType\n }\n }\n }\n }\n checkoutUserErrors {\n message\n code\n field\n }\n }\n}" #### Graphql Input { "checkoutId": "gid://shopify/Checkout/9234567890qwerty", "discountCode": "fixed_amount_off_code" } #### Graphql Response { "data": { "checkoutDiscountCodeApplyV2": null }, "errors": [ { "message": "Too many requests. Please try again in a few seconds", "locations": [ { "line": 2, "column": 3 } ], "path": [ "checkoutDiscountCodeApplyV2" ] } ] }