Invoices
Checkout invoice creation, updates, shipping assignment, and discount flows.
Module role in sales flow
invoices is the checkout state machine between cart and payment.
createbuilds checkout state from current cart credentials.- Address, shipping method, discount code, notes, and credit are all attached via invoice endpoints.
paymentsdepends on an existing invoice.
V2 contract notes
getuses therefreshcontract under the hood in v2 (POST /api/v2/invoices/{id}/refresh).create/refreshrequire a cart identifier from stored cart credentials.assignShippingMethodexpects eachinvoiceItemIdsvalue as a string.addCreditgenerally requires authenticated user context inAuthorization.shippingAddressis normalized by SDK to remove duplicated/noisy fields.Invoiceresponses intentionally excludeuser/userDataprofile objects.
Methods
Get
Use: invoices.get(options?).
Returns current invoice using stored cart + invoice credentials.
| Parameter | Type | Required | Description |
|---|---|---|---|
options | RequestOptions | Optional | Per-request controls. |
const invoiceRes = await client.invoices.get();Create
Use: invoices.create(options?).
Creates invoice from current cart credentials.
| Parameter | Type | Required | Description |
|---|---|---|---|
options | RequestOptions | Optional | Per-request controls. |
await client.cart.addItem(12001, 1);
const invoiceRes = await client.invoices.create();Refresh
Use: invoices.refresh(options?).
Syncs invoice with cart state.
await client.cart.addItem(12009, 1);
const refreshedInvoice = await client.invoices.refresh();Add Shipping Address
Use: invoices.addShippingAddress(shippingAddressId, shippingAddressIdentifier, options?).
| Parameter | Type | Required | Description |
|---|---|---|---|
shippingAddressId | number | Required | Shipping address ID. |
shippingAddressIdentifier | string | Required | Shipping address identifier. |
options | RequestOptions | Optional | Per-request controls. |
const addressRes = await client.shipping.createAddress({
firstName: 'John',
lastName: 'Doe',
regionId: 1,
cityId: 10,
address: 'No. 10, Example St',
});
if (addressRes.data) {
await client.invoices.addShippingAddress(
addressRes.data.id,
addressRes.data.identifier
);
}Add Discount Code
Use: invoices.addDiscountCode(code, options?).
| Parameter | Type | Required | Description |
|---|---|---|---|
code | string | Required | Discount code (uppercased by SDK). |
options | RequestOptions | Optional | Per-request controls. |
Returns: SazitoResponse<Invoice>.
const res = await client.invoices.addDiscountCode('WELCOME10');
if (res.error) {
// Invalid / expired code — show res.error.message to the user.
console.warn(res.error.message); // e.g. "کد تخفیف معتبر نیست"
}Error response. When the code is invalid or expired, the backend replies with a single field:
{ "message": "کد تخفیف معتبر نیست" }The SDK surfaces it on res.error:
| Field | Type | Notes |
|---|---|---|
message | string | Backend message, ready to display (localized server-side, e.g. کد تخفیف معتبر نیست). |
type | 'api' | Invalid/expired code is an API error. |
status | number | HTTP status from the gateway. |
details | JsonValue | Raw backend body ({ message }). |
Assign Shipping Method
Use: invoices.assignShippingMethod(shippings, options?).
| Field | Type | Required | Description |
|---|---|---|---|
shippings[].rateId | number | Required | Shipping rate ID. |
shippings[].invoiceItemIds | Array<string | number> | Required | Invoice item IDs covered by this rate (sent as strings to backend). |
options | RequestOptions | Optional | Per-request controls. |
const shippingMethodsRes = await client.invoices.getApplicableShippingMethods();
const assignments = shippingMethodsRes.data?.itemsShippingRate.map((entry) => ({
rateId: entry.shippingRate.id,
invoiceItemIds: [entry.invoiceItemId],
})) ?? [];
if (assignments.length > 0) {
await client.invoices.assignShippingMethod(assignments);
}Add Details
Use: invoices.addDetails(comment, options?).
| Parameter | Type | Required | Description |
|---|---|---|---|
comment | string | Required | Customer comment for invoice. |
options | RequestOptions | Optional | Per-request controls. |
await client.invoices.addDetails('Please deliver between 14:00 and 16:00');Add Form
Use: invoices.addForm(input, options?).
Attaches checkout dynamic form data to invoice via /api/v2/invoices/{id}/add_form.
This is invoice-level checkout form data, not product form answers and not booking schedule fields.
| Field | Type | Required | Description |
|---|---|---|---|
input.formAttributes | Record<string, any> | Required | Checkout-level form payload (for invoice-level fields such as legal or delivery form steps). |
input.identifier | string | Optional | Explicit invoice identifier override. |
options | RequestOptions | Optional | Per-request controls. |
await client.invoices.addForm({
identifier: 'invoice-identifier-if-needed',
formAttributes: {
nationalCode: '0012345678',
agreeToTerms: true,
},
});Get Applicable Shipping Methods
Use: invoices.getApplicableShippingMethods(options?).
Returns: SazitoResponse<ApplicableShippingMethods>.
const shippingRatesRes = await client.invoices.getApplicableShippingMethods();Add Credit
Use: invoices.addCredit(options?).
Applies wallet credit to the current invoice. In most shops this flow requires authenticated user context.
await client.invoices.addCredit();Remove Credit
Use: invoices.removeCredit(options?).
Removes wallet credit from the current invoice.
await client.invoices.removeCredit();Toggle Credit
Use: invoices.toggleCredit(options?).
Toggles wallet credit based on current invoice.creditTotal.
await client.invoices.toggleCredit();Clear Invoice
Use: invoices.clearInvoice().
Clears persisted invoice credentials only.
client.invoices.clearInvoice();Invoice key fields
| Field | Type | Required | Description |
|---|---|---|---|
id | number | Required | Invoice ID. |
identifier | string | Required | Invoice identifier token. |
items | InvoiceItem[] | Required | Invoice items. |
shippingAddress | InvoiceShippingAddress | Optional | Selected address. |
shippingItems | ShippingItem[] | Required | Shipping assignments. |
netTotal | number | Required | Net total from backend invoice pricing. |
finalTotal | number | Required | Final checkout total. |
creditTotal | number | Required | Applied wallet credit amount. |
discountCode | string | Optional | Applied discount code. |
For user profile data, use users.getCurrentUser() instead of relying on invoice payloads.
Known errors
| Type | When it happens |
|---|---|
validation | Missing cart/invoice credentials for dependent steps. |
api | Invalid or expired discount code (addDiscountCode); error.message carries the server text. |
Examples
// app/lib/sazito-client.ts
import { createSazitoClient } from '@sazito/client-sdk';
export const sazito = createSazitoClient({
domain: process.env.NEXT_PUBLIC_SAZITO_DOMAIN!,
});// app/[lang]/(home)/page.tsx
'use client';
import { useEffect, useState } from 'react';
import { sazito } from '@/app/lib/sazito-client';
export default function Example() {
const [data, setData] = useState<any>(null);
useEffect(() => {
(async () => {
await sazito.cart.addItem(12001, 1);
const res = await sazito.invoices.create();
setData(res.data ?? null);
})();
}, []);
return <pre>{JSON.stringify(data, null, 2)}</pre>;
}Type Details
The following tables are generated from SDK TypeScript types and include nested object fields.
ShippingAssignment
Source: src/types/shipping.ts
| Field | Type | Required |
|---|---|---|
rateId | number | Required |
invoiceItemIds | Array<string | number> | Required |
ApplicableShippingMethods
Source: src/types/shipping.ts
| Field | Type | Required |
|---|---|---|
shippingMethods | ShippingMethod[] | Required |
groupedShippingRates | Record<string, ShippingRate[]> | Required |
itemsShippingRate | ItemShippingRate[] | Required |
Invoice
Source: src/types/invoice.ts
| Field | Type | Required |
|---|---|---|
id | number | Required |
identifier | string | Required |
items | InvoiceItem[] | Required |
shippingAddress | InvoiceShippingAddress | Optional |
shippingItems | ShippingItem[] | Required |
needsShipping | boolean | Required |
userComment | string | Optional |
netTotal | number | Required |
finalTotal | number | Required |
vat | number | Required |
vatPercent | number | Required |
itemsDiscount | number | Required |
discountTotal | number | Required |
customerProfit | number | Required |
customerProfitPercentage | number | Required |
itemsTotalRawPrice | number | Required |
couponTotal | number | Required |
shippingTotal | number | Required |
creditTotal | number | Required |
discountUsages | Array<{ discountCode: { code: string; userSegment?: string; }; }> | Required |
discountUsages.[].discountCode | { code: string; userSegment?: string; } | Required |
discountUsages.[].discountCode.code | string | Required |
discountUsages.[].discountCode.userSegment | string | Optional |
coupon | { userSegment?: string; } | Optional |
coupon.userSegment | string | Optional |
discountCode | string | Optional |
InvoiceItem
Source: src/types/invoice.ts
| Field | Type | Required |
|---|---|---|
id | number | string | Required |
productVariantId | number | Required |
name | string | Required |
quantity | number | Required |
unitPrice | number | Required |
lineTotal | number | Required |
rawPrice | number | Required |
customerProfit | number | Required |
image | Image | Required |
product | CheckoutProductSnapshot | Required |
commercialFiles | any | Optional |
formAttributes | Record<string, FormAttributeValue> | InvoiceItemFormAttributes | Optional |
bookingAttributes | SchedulerBookingAttributes | Optional |
formFields | Record<string, any> | Optional |
ShippingItem
Source: src/types/invoice.ts
| Field | Type | Required |
|---|---|---|
invoiceItemIds | Array<number | string> | Required |
rate | { id: number; name: string; price: number; icon?: string; color?: string; type?: string; } | Required |
rate.id | number | Required |
rate.name | string | Required |
rate.price | number | Required |
rate.icon | string | Optional |
rate.color | string | Optional |
rate.type | string | Optional |
ShippingAddress
Source: src/types/invoice.ts
| Field | Type | Required |
|---|---|---|
id | number | Required |
identifier | string | Required |
firstName | string | Required |
lastName | string | Required |
mobilePhone | string | Optional |
phoneNumber | string | Optional |
email | string | Optional |
region | Region | Optional |
city | City | Required |
address | string | Required |
postalCode | string | Optional |
latitude | number | Optional |
longitude | number | Optional |
userSetCoordinatesBefore | boolean | Optional |