DashForDevsDocs
DocsNewsAPI v1
dashfordevs.comSign in

© 2026 DashForDevs

DocsNewsdashfordevs.comLegalStatus
DocsCartApply Discount Code
Introduction
Getting Started
Authentication
Products
Categories
Cart
Get CartAdd to CartUpdate Cart ItemRemove from CartApply Discount CodeSubscription Cart Lines
Checkout
Blog
Reviews
Webhooks
Customers
Orders
Inventory
Shipping
Discounts
Affiliates
Email
Media & Files
Analytics & Tracking
Locations
Tax
Global Data
SEO
Error Handling
Geography
Finance
Automations
Page Types
Surveys
Billing & Subscriptions
Storefront Payments
SDK Reference
REST API
Storefront Architecture
Notifications
Security
Team & Permissions
Migrations & Import
Calendar & Appointments
Contact Forms
AI Bots
Dashboard API
Deployment
Changelog
Troubleshooting
Recipes
CLI & Deployment
Image Pipeline
White-label Platforms
Records Engine
Platform Modules
Platform Security
Site Keys & Client Sites
Use Cases
Operations

Apply Discount Code

Apply a coupon or affiliate code to the cart.

Updated March 29, 2026

On this page
  • Overview
  • SDK Usage
  • Validate Before Applying
  • Discount Code Input Example
  • REST API
  • Next Steps

Overview

Apply a discount or coupon code to the cart. The discount is validated and applied server-side.

SDK Usage

typescript
import dash from "@/lib/dash"

// Apply discount
const { message, cart } = await dash.cart.applyDiscount("SAVE20")
// cart.discount_code = "SAVE20"
// cart.discount_amount = "15.00"

// Remove discount
const result = await dash.cart.removeDiscount()

Validate Before Applying

You can validate a code before applying it to show the customer what they'll save:

typescript
// This doesn't apply the code — just checks it
const response = await fetch("/storefront/discount/validate", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "X-API-Key": apiKey,
  },
  body: JSON.stringify({
    code: "SAVE20",
    subtotal: "99.99",
  }),
})

const result = await response.json()
// result = {
//   valid: true,
//   discount_amount: "15.00",
//   is_percentage: true,
//   rate: "20",
//   code: "SAVE20"
// }

Discount Code Input Example

typescript
"use client"
import { useState } from "react"
import dash from "@/lib/dash"

export function DiscountCodeInput({ onApplied }) {
  const [code, setCode] = useState("")
  const [error, setError] = useState("")

  async function handleApply() {
    try {
      setError("")
      const { cart } = await dash.cart.applyDiscount(code)
      onApplied(cart)
    } catch (err) {
      setError(err.message) // "Invalid code" or "Code expired"
    }
  }

  return (
    <div>
      <input value={code} onChange={e => setCode(e.target.value)}
             placeholder="Discount code" />
      <button onClick={handleApply}>Apply</button>
      {error && <p className="text-red-500">{error}</p>}
    </div>
  )
}

REST API

shell
# Apply discount
POST /storefront/cart/{cart_id}/apply-discount
Content-Type: application/json
X-API-Key: pk_live_your_key

{ "code": "SAVE20" }

# Remove discount
POST /storefront/cart/{cart_id}/remove-discount
X-API-Key: pk_live_your_key

# Validate without applying
POST /storefront/discount/validate
Content-Type: application/json
X-API-Key: pk_live_your_key

{ "code": "SAVE20", "subtotal": "99.99" }

Next Steps

  • Discount Codes — Types of discounts available
  • Create Order — Proceed to checkout with discount applied
  • Validate Code — Code validation details
PreviousRemove from CartNextSubscription Cart Lines

On this page

  • Overview
  • SDK Usage
  • Validate Before Applying
  • Discount Code Input Example
  • REST API
  • Next Steps