DashForDevsDocs
DocsNewsAPI v1
dashfordevs.comSign in

© 2026 DashForDevs

DocsNewsdashfordevs.comLegalStatus
DocsCartAdd to Cart
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

Add to Cart

Add a product to the cart with quantity and variant.

Updated September 29, 2026

On this page
  • Overview
  • SDK Usage
  • Parameters
  • Add to Cart Button Example
  • REST API
  • Error Handling
  • Next Steps

Overview

Add a product size to the cart. If no cart exists yet, one is created automatically.

SDK Usage

typescript
import dash from "@/lib/dash"

const { cart_id, item } = await dash.cart.add({
  productId: "prod_abc123",
  sizeId: "size_1g",
  quantity: 1,
})

Parameters

ParameterTypeRequiredDescription
productIdstringYesThe product ID
sizeIdstringYesThe specific size/variant ID
quantitynumberNoQuantity to add (default: 1)
freestyleSelectionsarrayNoCustom selections for freestyle/bundle products
subscriptionPlanIdstringNoStart a subscription instead of buying once. A plan id from size.subscription_plans; see Subscription Cart Lines
Important: You must pass a sizeId, not just a product ID. Every purchasable item is a specific size within a product. Get available sizes from the product variations endpoint.

Add to Cart Button Example

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

export function AddToCartButton({ productId, sizeId }: {
  productId: string
  sizeId: string
}) {
  const [loading, setLoading] = useState(false)
  const [added, setAdded] = useState(false)

  async function handleAdd() {
    setLoading(true)
    try {
      await dash.cart.add({ productId, sizeId, quantity: 1 })
      setAdded(true)
      setTimeout(() => setAdded(false), 2000)
    } catch (err) {
      alert(err.message)
    }
    setLoading(false)
  }

  return (
    <button onClick={handleAdd} disabled={loading}>
      {added ? "Added!" : loading ? "Adding..." : "Add to Cart"}
    </button>
  )
}

REST API

shell
POST /storefront/cart/add
X-API-Key: pk_live_your_key
Content-Type: application/json

{
  "product_id": "prod_abc123",
  "size_id": "size_1g",
  "quantity": 1,
  "cart_id": "cart_xyz"  // optional — omit to create new cart
}

Error Handling

Common errors when adding to cart:

  • Out of stock — The size has no inventory left
  • Invalid size — The size ID doesn't belong to the product
  • Quantity exceeds stock — Requested more than available
typescript
try {
  await dash.cart.add({ productId, sizeId, quantity: 5 })
} catch (err) {
  if (err.message.includes("stock")) {
    // Show stock warning
  }
}

Next Steps

  • Update Cart Item — Change quantities
  • Remove from Cart — Remove items
  • Recipe: Build a Cart Drawer — Full cart UI walkthrough
PreviousGet CartNextUpdate Cart Item

On this page

  • Overview
  • SDK Usage
  • Parameters
  • Add to Cart Button Example
  • REST API
  • Error Handling
  • Next Steps