@flatfile/api

中文文档
1.23.0发布于 2025/08/29TypeScriptCommonJS近 30 天下载 95.9万

npm shield [fern shield](https://buildwithfern.com/?utm_source=flatfilers/f

原始使用文档
文档目录8 节
  1. Documentation
  2. Installation
  3. Usage
  4. Handling errors
  5. Handling events
  6. Fields
  7. Contributing
  8. License

Flatfile Node API Library

npm shield fern shield

The Flatfile Node.js library provides convenient access to the Flatfile API from JavaScript/TypeScript.

Documentation

API reference documentation is available here.

Installation

npm install --save @flatfile/api
# or
yarn add @flatfile/api

Usage

Try it out

import { FlatfileClient } from '@flatfile/api';

async function main() {
  const flatfile = new FlatfileClient({
    // This is usually the environment specific "Secret Key" that can be found
    // on the Getting Started page in the Flatfile dashboard.
    token: 'YOUR_API_KEY',
  });

  const workbook = await flatfile.workbooks.create({
    name: 'SDK Example',
    sheets: [
      {
        name: 'Contacts',
        slug: 'contacts',
        fields: [
          {
            key: 'firstName',
            type: 'string',
            label: 'First Name',
          },
          {
            key: 'lastName',
            type: 'string',
            label: 'Last Name',
          },
          {
            key: 'email',
            type: 'string',
            label: 'Email',
          },
        ],
        actions: [
          {
            slug: 'submitAction',
            label: 'Submit',
            type: 'string',
            description: 'Submit data to webhook.site',
            primary: true,
          },
        ],
      },
    ],
  });

  console.log('Created workbook with id:', workbook.data.id);
}

Handling errors

When the API returns a non-success status code (4xx or 5xx response), a subclass of FlatfileError will be thrown:

try {
  await client.agents.get("environment-id", "agent-id");
} catch (err) {
  if (err instanceof FlatfileError) {
    console.log(err.statusCode); // 400
    console.log(err.message); // "BadRequestError"
    console.log(err.body); // list of errors
  }
}

Error codes are as followed:

Status CodeError Type
400BadRequestError
404NotFoundError

Handling events

The flatfile platform emits different events (e.g. user:added, webhook:removed). The SDK uses discriminated unions that make it easy to handle specific events.

const event = eventResponse.data;
if (event.type === 'job:ready') {
  console.log(event.payload.operation); // FILE
  console.log(event.payload.type); // PIPELINE
} else if (event.type === 'records:created') {
  console.log(event.payload.count) // 100
}

Fields

The SDK uses discriminated unions that make it easy to introspect different fields.

for (const field of sheet.config.fields) {
    if (field.type === 'boolean') {
      console.log(field.config?.allowIndeterminate); // false
    } else if (field.type === "number") {
      console.log(field.config?.decimalPlaces); // 2
    } else if (field.type === "enum") {
      console.log(field.config.allowCustom); // true
    }
}

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.

版本与发布记录

@flatfile/api 共发布 179 个版本,首次发布于 2022/11/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.23.12025/11/061 年前
1.23.1-conditional-submit-routing.02025/08/301.2 年前
1.23.0latest2025/08/291.2 年前
1.22.02025/08/131.3 年前
1.21.02025/08/021.3 年前
1.20.02025/07/231.3 年前

其他发布通道:rc → 1.15.7-rc.1。

关于 @flatfile/api 的常见问题

@flatfile/api 是什么?

[](https://buildwithfern.com/?utm_source=flatfilers/f

如何安装 @flatfile/api?

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

@flatfile/api 支持 TypeScript 吗?

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

@flatfile/api 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@flatfile/api 可以商用吗?

@flatfile/api 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@flatfile/api 的下载量有多少?

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

@flatfile/api 还在维护吗?

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