@crvouga/mockingbird-service-klaviyo

中文文档
3.10.7发布于 2026/11/03MITTypeScriptES Module构建来源已验证近 30 天下载 2616

Stateful emulator of the Klaviyo events API: JSON:API event create (Ordered Product, Placed Order), event reads, unique_id dedupe, JSON:API errors and an outbox.

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. Routes
  4. Admin (beyond the standard contract)
  5. Namespaces
  6. Deliberately not modelled
  7. API

@crvouga/mockingbird-service-klaviyo

Local emulators. Real API contracts. Part of Mockingbird.

Stateful emulator of the Klaviyo events API for test suites: the JSON:API create-event endpoint our backend posts Ordered Product and Placed Order to after checkout, the event reads, and an outbox a suite asserts on. Errors come back in Klaviyo's JSON:API errors[] shape, so the text our client throws (and logs) is the text the real API would produce.

  • Operation coverage: SUPPORT.md
  • The contract (openapi.yaml) is trimmed from Klaviyo's published API (revision 2024-02-15, the one our backend pins) to what our consumer sends.

Install

npm install -D @crvouga/mockingbird-service-klaviyo

ESM only. Node >= 22 or Bun >= 1.2. No native dependencies. Serve it with npx mockingbird-klaviyo serve, createServer from ./server (Node), or createRuntime with any Fetch server.

Usage

Point KLAVIYO_URL (Joi-required, fully overridable) at the emulator's event endpoint; any KLAVIYO_API_KEY works.

npx mockingbird-klaviyo serve --port 8811
# KLAVIYO_URL=http://127.0.0.1:8811/api/events/
import { ApiKeySession, EventEnum, EventsApi, MetricEnum, ProfileEnum } from "klaviyo-api"
import { createServer } from "@crvouga/mockingbird-service-klaviyo/server"

const klaviyo = await createServer()

// The application's Klaviyo client. Only basePath changes.
const events = new EventsApi(new ApiKeySession("any-key"))
events.basePath = klaviyo.url
await events.createEvent({
  data: {
    type: EventEnum.Event,
    attributes: {
      properties: { OrderId: "1001" },
      value: 29,
      uniqueId: "order-1001",
      metric: { data: { type: MetricEnum.Metric, attributes: { name: "Placed Order" } } },
      profile: { data: { type: ProfileEnum.Profile, attributes: { email: "[email protected]" } } },
    },
  },
})

// Test assertion: the outbox holds what the emulator accepted.
const response = await fetch(`${klaviyo.url}/__admin/[email protected]&metric=Placed%20Order`)
const { messages } = (await response.json()) as { messages: { metric: string; value: number }[] }
// messages → [{ metric: "Placed Order", value: 29, … }]

await klaviyo.close()

Routes

RouteBehaviour
POST /api/events/JSON:API {data: {type: "event", attributes: {properties, time?, value?, value_currency?, unique_id?, metric: {data: {type: "metric", attributes: {name}}}, profile: {data: {type: "profile", id?, attributes: {email?, phone_number?, external_id?}}}}}} → 202, no body. The profile is matched by id, email, phone or external id (else created); the metric is created on first use. A repeat of metric + profile + unique_id is accepted but not stored twice (a BullMQ retry does not double revenue). Invalid input is 400 {errors: [{id, status, code: "invalid", title, detail, source: {pointer}}]}; a phone number that is not E.164 is rejected with Klaviyo's wording.
GET /api/events/{data: [event], links: {self, next: null, prev: null}}, oldest first. Each event carries attributes.{timestamp, event_properties (properties + $value + $event_id), datetime, uuid} and relationships.{profile, metric}.
GET /api/events/{id}/One event, or 404 not_found.

Every call needs Authorization: Klaviyo-API-Key <key> (else 401 not_authenticated) and a revision: YYYY-MM-DD header (else 400). Paths answer with or without the trailing slash.

Admin (beyond the standard contract)

RouteEffect
GET /__admin/outbox?to=&since=&metric=&unique_id=Accepted events of the calling namespace, oldest first (to matches the profile's email, phone or id). GET /__admin/outbox/:id for one.
GET /__admin/profilesProfiles created or matched by events.

Fault presets (POST /__admin/faults {"preset": "<name>", "count"?: n}; GET /__admin/faults/presets): throttled (429 + Retry-After: 1), invalid_api_key (401), server_error (500), service_unavailable (503), connection_drop (the socket closes; nothing is stored).

Namespaces

x-mockingbird-namespace, a /__admin/ns/<name> prefix in KLAVIYO_URL, or by private key: PUT /__admin/credentials {"credentials": {"<KLAVIYO_API_KEY>": "<namespace>"}}.

Deliberately not modelled

  • Flows, lists, segments and campaigns: an event never triggers an email (use the outbox).
  • Profile-id validation: Klaviyo profile ids are Klaviyo-generated; our backend sends the user token as profile.data.id, which the emulator adopts as the profile id rather than rejecting. Whether the real API accepts an unknown id is unverified (no sandbox credentials).
  • Pagination (page[cursor]), filter, fields[…] and include on reads.
  • Rate limits, except through the throttled preset.

API

ExportKindDescription
KlaviyoAPIclassThe in-process emulator: fetch(request), reset(), events(), state. Options: sqlite, now, namespace, baseUrl.
createRuntimefunctionThe emulator with the full service contract (health, admin, namespaces, credentials, presets, outbox). Options: baseUrl, clock, seed, adminKey, onLog, sqlite.
KLAVIYO_PRESETSobjectEvery named fault preset.
KLAVIYO_NAMESPACEstringThe service name, "klaviyo".
KLAVIYO_REVISIONstringThe API revision our backend sends, "2024-02-15".
klaviyoApiKeyfunctionThe key in Authorization: Klaviyo-API-Key <key> (how credentials map to namespaces).
klaviyoErrorfunctionBuild a JSON:API {errors: [...]} body.
document, operationIds, supportedOperationIdsvaluesThe vendored OpenAPI contract and its operation ids.
createServer, serveTarget, DEFAULT_PORT (./server)NodeServe over node:http; the serve CLI target; port 8811.

Part of mockingbird.

版本与发布记录

@crvouga/mockingbird-service-klaviyo 共发布 39 个版本,首次发布于 2026/09/23;过去 12 个月发布了 39 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.10.7latest2026/11/038 天前
3.10.62026/11/038 天前
3.10.52026/11/038 天前
3.10.42026/11/038 天前
3.10.32026/11/038 天前
3.10.22026/11/038 天前

关于 @crvouga/mockingbird-service-klaviyo 的常见问题

@crvouga/mockingbird-service-klaviyo 是什么?

Stateful emulator of the Klaviyo events API: JSON:API event create (Ordered Product, Placed Order), event reads, unique_id dedupe, JSON:API errors and an outbox.

如何安装 @crvouga/mockingbird-service-klaviyo?

在项目目录运行 npm install @crvouga/mockingbird-service-klaviyo,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 mockingbird-klaviyo,可以用 npx mockingbird-klaviyo 直接运行。

@crvouga/mockingbird-service-klaviyo 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@crvouga/mockingbird-service-klaviyo 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@crvouga/mockingbird-service-klaviyo 可以商用吗?

@crvouga/mockingbird-service-klaviyo 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@crvouga/mockingbird-service-klaviyo 的下载量有多少?

根据 npm 官方下载统计,@crvouga/mockingbird-service-klaviyo 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,616 次,日均约 87 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@crvouga/mockingbird-service-klaviyo 还在维护吗?

最新版本 3.10.7 发布于 2026/11/03(8 天前);至今共发布 39 个版本,其中过去 12 个月发布了 39 个;首次发布于 2026/09/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。