@fluid-app/api-client

中文文档
0.1.19发布于 2025/07/16MITTypeScriptESM + CJS近 30 天下载 2.1万

Fluid Commerce API client for JavaScript applications

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. Basic Example
  4. Available API Functions
  5. Function Patterns
  6. TypeScript Support
  7. Integration with Other Packages
  8. Features
  9. Environment Configuration
  10. Local Development
  11. License

@fluid-app/api-client

A JavaScript client for the Fluid Commerce API, providing typed access to all API endpoints.

Installation

# Using npm
npm install @fluid-app/api-client

# Using yarn
yarn add @fluid-app/api-client

# Using pnpm
pnpm add @fluid-app/api-client

Usage

Basic Example

import { createCart, getCart } from "@fluid-app/api-client";

// Create a new cart
const newCart = await createCart({
  request: {
    fluid_shop: "your-shop-id", // Replace with your shop ID
    country_code: "US",
    items: [
      {
        variant_id: 11601, // Replace with your variant ID
        quantity: 1,
      },
    ],
  },
});

// Get cart details
const cartDetails = await getCart({
  params: {
    cart_token: newCart.cart.cart_token,
  },
});

Available API Functions

The client provides typed functions for all Fluid Commerce API endpoints, automatically generated from the OpenAPI specification.

Cart Management

import {
  addCartItem,
  createCart,
  getCart,
  removeCartItem,
} from "@fluid-app/api-client";

// Create a cart
const cart = await createCart({
  request: {
    fluid_shop: "your-shop-id",
    country_code: "US",
  },
});

// Add an item to cart
await addCartItem({
  params: {
    cart_token: cart.cart.cart_token,
  },
  request: {
    variant_id: 12345,
    quantity: 1,
  },
});

// Remove an item from cart
await removeCartItem({
  params: {
    cart_token: cart.cart.cart_token,
    item_id: "123", // ID of the cart item (not the variant ID)
  },
});

FairShare Analytics

import { registerSession, trackCheckoutStarted } from "@fluid-app/api-client";

// Register a new session
const session = await registerSession({
  request: {
    fluid_shop: "your-shop-id",
  },
});

// Track checkout started event
await trackCheckoutStarted({
  request: {
    cart_token: "cart_abc123",
    metadata: {
      fluid_shop: "your-shop-id",
      fluid_session: session.session_token,
      attribution: {
        username: "influencer-name",
      },
    },
  },
});

Function Patterns

All API functions follow a consistent pattern:

// For endpoints with path parameters and request body
functionName({
  params: {
    /* path parameters */
  },
  request: {
    /* request body */
  },
});

// For endpoints with only path parameters
functionName({
  params: {
    /* path parameters */
  },
});

// For endpoints with only request body
functionName({
  request: {
    /* request body */
  },
});

// For endpoints with no parameters
functionName();

All functions return a Promise with the typed response from the API.

TypeScript Support

The client provides full TypeScript support, including:

import type { ApiTypes, Schemas } from "@fluid-app/api-client";

// Access request types
type CreateCartRequest = ApiTypes["cart-create"]["request"];

// Access response types
type CartResponse = ApiTypes["cart-get"]["response"];

// Access schema types
type CartItem = Schemas["CartItem"];

Integration with Other Packages

This package is used by:

  • @fluid-app/fluid: The main SDK that provides a high-level interface for cart management and checkout
  • @fluid-app/fairshare: The tracking SDK that uses the API client to send events and manage sessions

Most applications should use the higher-level @fluid-app/fluid package instead of using this API client directly.

Features

  • ✅ Full TypeScript support with accurate types for requests and responses
  • ✅ Automatic error handling with helpful error messages
  • ✅ Requests automatically use the correct HTTP methods and paths
  • ✅ Generated from the official Fluid Commerce API specification
  • ✅ Configurable environment variables with validation

Environment Configuration

You can configure the API client behavior by setting the following environment variables:

VariableDefaultDescription
FLUID_API_BASE_URLhttps://api.fluid.appThe base URL for the Fluid API
FLUID_API_TIMEOUT30000Request timeout in milliseconds
FLUID_API_DEBUGfalseEnable debug logging (set to true to enable)

Example:

# .env file or environment variables
FLUID_API_BASE_URL=https://api.staging.fluid.app
FLUID_API_TIMEOUT=60000
FLUID_API_DEBUG=true

All environment variables are validated using T3 Env and provide proper TypeScript types.

Local Development

For local development, create a .env file in the package root with the following content:

# Local development configuration
FLUID_API_BASE_URL=http://localhost:3000
FLUID_API_TIMEOUT=60000
FLUID_API_DEBUG=true

You can create this file with:

echo 'FLUID_API_BASE_URL=http://localhost:3000
FLUID_API_TIMEOUT=60000
FLUID_API_DEBUG=true' > .env

This will configure the client to use your local API server with extended timeout and debug logging enabled.

License

MIT

版本与发布记录

@fluid-app/api-client 共发布 20 个版本,首次发布于 2025/03/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.19latest2025/07/161.3 年前
0.1.182025/07/161.3 年前
0.1.172025/06/031.5 年前
0.1.162025/05/291.5 年前
0.1.152025/05/231.5 年前
0.1.142025/05/221.5 年前

关于 @fluid-app/api-client 的常见问题

@fluid-app/api-client 是什么?

Fluid Commerce API client for JavaScript applications

如何安装 @fluid-app/api-client?

在项目目录运行 npm install @fluid-app/api-client,或使用 pnpm add / yarn add / bun add。

@fluid-app/api-client 支持 TypeScript 吗?

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

@fluid-app/api-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fluid-app/api-client 可以商用吗?

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

@fluid-app/api-client 的下载量有多少?

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

@fluid-app/api-client 还在维护吗?

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