Swell integration

Tariffs API for Swell.

Add US duty calculation to a Swell.is headless commerce store via a Node middleware that calls Tariffs API and writes the duty back to the cart.

Swell is a US headless commerce platform popular with DTC brands that want full checkout control. The integration is a small Node service (or Next.js route handler) that intercepts cart updates, calls Tariffs API per line, and pushes the duty back to Swell as a discount or shipping adjustment.

Why this works

Built for Swell developers.

Headless by design

Swell expects you to own the checkout. Slotting in a duty API is the kind of integration the platform was built for.

Node or Next.js

Use the Swell JS SDK on your server, fetch the cart, call Tariffs API, post the adjustment back. Half a screen of code.

Per-item or aggregate

Push duty as one custom line item per cart or as a single aggregate amount. Both supported by Swell's cart model.

Public pricing

$199/month flat, 100,000 API calls. No quote, no contract, no demo.

In your code

Drop it in.

One end-to-end example: a Next.js route handler that's called on cart update.

1. Cart-update handler (Next.js)

typescript
// app/api/cart-duty/route.ts
import swell from "swell-node"

swell.init(process.env.SWELL_STORE_ID!, process.env.SWELL_API_KEY!)
const TA = process.env.TARIFFSAPI_KEY!

export async function POST(req: Request) {
  const { cartId } = await req.json()
  const cart = await swell.get(`/carts/${cartId}`)

  let dutyTotal = 0
  for (const item of cart.items) {
    const hts = item.product?.attributes?.hts_code
    const origin = item.product?.attributes?.country_of_origin
    if (!hts || !origin) continue

    const r = await fetch(
      `https://tariffsapi.com/api/v1/tariffs/resolve?hts=${hts}&origin=${origin}`,
      { headers: { Authorization: `Bearer ${TA}` } }
    )
    const data = await r.json()
    const rate = data.summary.total_resolved_ad_valorem_rate / 100
    dutyTotal += item.price * item.quantity * rate
  }

  await swell.put(`/carts/${cartId}`, {
    shipment_price: (cart.shipment_price || 0) + dutyTotal,
    metadata: { us_duty: dutyTotal },
  })

  return Response.json({ duty: dutyTotal })
}
What you get back

One endpoint. Deterministic JSON.

Resolve returns the merged US duty rate per HTS code and origin pair. For multi-item carts, batch via resolve_batch (up to 200 codes per call) to keep the round trip under 500ms.

GET /api/v1/tariffs/resolve?hts=8541.10.00.80&origin=CN

{
  "summary": {
    "applicable_ad_valorem_rate": 0.0,
    "resolved_additional_ad_valorem_rate": 25.0,
    "total_resolved_ad_valorem_rate": 25.0
  },
  "base_tariff": { "percentage_component": 0.0 },
  "additional_measures": [
    {
      "program": "section_301",
      "chapter_99_code": "9903.91.05",
      "resolved_rate": { "percentage_component": 0.25 }
    }
  ]
}
FAQ

Swell + Tariffs API

Where do I store HTS codes in Swell?

Product attributes. Swell supports custom attributes per product and variant. Set hts_code and country_of_origin and read them from item.product.attributes in your handler.

Where should I display the duty in the storefront?

Most teams render it as a discount line item (clear to the customer) or fold it into shipping. Both flow naturally through Swell's order totals.

Can I trigger this from Swell's webhooks?

Yes. Swell exposes cart.updated webhooks. Subscribe to them and re-run the duty calc on relevant changes. Many teams call it client-side first for instant UX, then verify server-side at checkout.

How does this compare to a hosted DDP service?

Hosted DDP (Zonos, Passport) bundles classification, shipping, and tax. Tariffs API is the duty number only. If you already have classification done and ship via your own carrier accounts, this is the cheaper, lighter integration.

Sources

Add US duty to your Swell checkout in an afternoon.