메인 콘텐츠로 건너뛰기

SDK

타입 안전하고 프로덕션에 바로 사용할 수 있는 Headless Commerce API 클라이언트입니다.

설치

npm install @headless-commerce/sdk

빠른 시작

import { HeadlessCommerce } from '@headless-commerce/sdk';

// Admin 클라이언트 (서버 사이드)
const admin = new HeadlessCommerce({
  secretKey: 'sk_test_your_key',
});

// Storefront 클라이언트 (클라이언트 사이드에서 안전하게 사용 가능)
const storefront = new HeadlessCommerce({
  publishableKey: 'pk_test_your_key',
});

사용 예시

상품 목록 조회

const { data, has_more } = await storefront.products.list({
  status: 'active',
  limit: 20,
});
// data: Product[] — 자동 완성이 지원되는 완전한 타입

장바구니 플로우

// 장바구니 생성
const cart = await storefront.carts.create({
  session_id: 'guest-session-123',
});

// 상품 추가
await storefront.carts.addItem(cart.id, {
  variant_id: 'var_001',
  quantity: 2,
});

// 계산된 합계가 포함된 장바구니 조회
const updated = await storefront.carts.get(cart.id);
console.log(updated.summary.total);
// { amount: 78000, currency: "KRW" }

// 체크아웃
const order = await storefront.carts.checkout(cart.id, {
  email: 'customer@example.com',
  payment_provider: 'stripe',
  payment_method_id: 'pm_xxx',
});

관리자 작업

// 옵션이 포함된 상품 생성
const product = await admin.products.create({
  name: 'Classic T-Shirt',
  type: 'physical',
  status: 'draft',
  options: [
    { name: 'Size', values: ['S', 'M', 'L'] },
    { name: 'Color', values: ['Black', 'White'] },
  ],
});

// 자동 페이지네이션
for await (const order of admin.orders.listAll({ status: 'confirmed' })) {
  console.log(order.number);
}

오류 처리

import { HeadlessCommerceError } from '@headless-commerce/sdk';

try {
  const product = await storefront.products.get('nonexistent');
} catch (error) {
  if (error instanceof HeadlessCommerceError) {
    console.log(`API 오류 ${error.status}: ${error.code}${error.message}`);
  }
}

웹훅 검증

import crypto from 'node:crypto';

function verifyWebhook(payload: string, signature: string, secret: string): boolean {
  const expected = crypto
    .createHmac('sha256', secret)
    .update(payload)
    .digest('hex');
  return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
}
전체 통합 패턴은 웹훅 가이드를 참고하세요.

설정

옵션타입필수설명
secretKeystring*서버 사이드용 비밀 API 키
publishableKeystring*클라이언트 사이드용 공개 키
baseUrlstring아니오커스텀 API 기본 URL (기본값: https://api.headlesscommerce.io/v1)
secretKey 또는 publishableKey 중 하나가 필수입니다.

리소스

총 23개의 API 리소스를 사용할 수 있습니다:
리소스스토어프론트관리자
productslist, get, get_by_slugCRUD, images, bundles
categorieslist, treeCRUD
collectionslist, getCRUD
cartscreate, get, items, discounts
checkoutcreate
orderslist, get, lookupCRUD, confirm, cancel, complete
customersme, addressesCRUD, tokens
variantsCRUD
inventoryget, adjust, set
fulfillmentscreate, ship, deliver
discountsvalidateCRUD
shipping_methodslistCRUD
paymentsrecord, complete
returnsrequest, list, getCRUD, approve, reject, receive
refundscreate
regionsCRUD, prices, currencies, locales, translations
webhooksCRUD, test
api_keysCRUD, rotate
organizationget, update, members
storeget, update
logslist, get, stats
dashboardstats