@chalk-ai/client

中文文档
1.31.13发布于 2026/08/06Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 1万

TypeScript client for Chalk

原始使用文档
文档目录9 节
  1. Installation
  2. Generate Types
  3. Usage
  4. Modern JavaScript ES6
  5. CommonJS
  6. Constructor options (gRPC)
  7. Migrating to the gRPC Client
  8. Supported environment variables
  9. Constructor options (Legacy)

@chalk-ai/client

npm version CI

TypeScript client for Chalk.

Installation

$ yarn add @chalk-ai/client

Generate Types

Starting in your Chalk project directory, run:

$ chalk codegen typescript --out=generated_types.ts

Usage

Modern JavaScript ES6

import { ChalkGRPCClient } from "@chalk-ai/client"
// import { ChalkClient } from "@chalk-ai/client";

// Import your generated types (recommended)
import { FeaturesType } from "local/generated_types";

// Alternatively, define features you want to pull using the client
// as an interface with the feature type and feature name.
interface FeaturesType {
  "user.id": string;
  "user.fraud_score": number;
}

const client = new ChalkGRPCClient<FeaturesType>();

const result = await client.query({
  inputs: {
    "user.id": "1",
  },
  outputs: ["user.fraud_score"],
});

// The property `.data` has auto-complete based on the
// list provided in `output` above. So if you try to pull
// a feature that wasn't requested, you will see an error
// in type checking.
console.log(result.data["user.fraud_score"].value);

CommonJS

var ChalkGRPCClient = require("@chalk-ai/client").ChalkGRPCClient;
// var ChalkClient = require("@chalk-ai/client").ChalkClient; 

interface FeaturesType {
  "user.id": string;
  "user.fraud_score": number;
}

var client = new ChalkGRPCClient<FeaturesType>();

client
  .query({
    inputs: {
      "user.id": "1",
    },
    outputs: ["user.fraud_score"],
  })
  .then((result) => {
    console.log(result.data["user.fraud_score"].value);
  });

Constructor options (gRPC)

See the HTTP Options if you are using the HTTP Client, but we encourage reading the migration guide.

import { ChalkGRPCClient } from "@chalk-ai/client"

const options: ChalkGRPCClientOpts = {/* ... */ };
const chalkClient = new ChalkGRPCClient(options);

export interface ChalkGRPCClientOpts {
  /**
   * Your Chalk Client ID. This value will be read from the _CHALK_CLIENT_ID environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientId?: string;

  /**
   * Your Chalk Client Secret. This value will be read from the _CHALK_CLIENT_SECRET environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientSecret?: string;

  /**
   * The URL of your chalk API server. Defaults to https://api.chalk.ai
   */
  apiServer?: string;

  /**
   * The environment that your client will run against. This value will be read from the _CHALK_ACTIVE_ENVIRONMENT environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  activeEnvironment?: string;

  /**
   * A custom fetch client that will replace the fetch polyfill used by default. Used by both the HTTP and gRPC
   * clients - the HTTP client for all calls, and the gRPC client for fetching credentials from the API server.
   *
   * If not provided, the client will use the default fetch polyfill (native fetch with node-fetch as a fallback).
   */
  fetch?: CustomFetchClient;

  /**
   * A custom fetch headers object that will replace the fetch Headers polyfill used by default. This is primarily for use
   * with a custom fetch client, and is not the preferred way to add additional headers to requests.
   *
   * If not provided, the client will use the default fetch Headers polyfill (native fetch with node-fetch as a fallback).
   */
  fetchHeaders?: typeof Headers;

  /**
   * The format to use for date-type data.
   *
   * Defaults to "ISO_8601" (in UTC), also supports "EPOCH_MILLIS" as number of milliseconds since epoch
   */
  timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;

  /**
   * By default, the client will try to directly connect to the query client using the metadata provided during
   * credentials exchange, which returns a mapping between environment and query server. If this option is
   * truthy, the client will skip this step.
   *
   * The queryServer option will take precedence over this option (i.e. this option is essentially useless).
   * *
   * Defaults to false.
   */
  skipQueryServerFromCredentialExchange?: boolean;

  /**
   * Passed to the internal grpc client upon initialization. Under the hood, Chalk uses the @grpc/grpc-js library
   * and this allows users finer control of their gRPC usage.
   *
   * See https://grpc.github.io/grpc/node/grpc.Client.html for more information on what is supported here.
   */
  grpcClientOptions?: Partial<ClientOptions>;
}

Migrating to the gRPC Client

While Chalk supports both an HTTP Client and a gRPC Client for interfacing with Chalk, we heavily encourage users to use the gRPC Client for better performance, as this targets a more performant gRPC Query Server. After setting up a gRPC Query Server in your Chalk Environment, changing to the gRPC Client can be done in a few steps:

  • Use the import ChalkGRPCClient instead of ChalkClient
  • Check to see if any of the following initialization options need to be changed:
    • The option useQueryServerFromCredentialExchange has been changed to skipQueryServerFromCredentialExchange and negated to better reflect the default behavior for the gRPC Query Server.
    • Check your Chalk Environment for any custom routing rules. If you are not sure, reach out to Chalk support for help here.
      • If your previous SDK client explicitly sets the engine-type: engine header on requests, this will need to be changed to engine-grpc (or omitted, the gRPC client will automatically set this header but respects all overrides).
      • It is most likely not necessary to change a provided QueryServer as most routing is done via SDK-set headers, but depending on your setup this may need to be directly specified if using a non-standard port - the gRPC query server listens on port 443 by default.
    • It is not necessary to remove the fetch or fetchHeaders as these are still used during some HTTP routines such as fetching credentials.

After these initialization changes are made, no changes to actual calls should be necessary - the gRPC Client uses the same function signatures.

At this moment, only query(), queryBulk(), and multiQuery() are supported - if your usage requires one of the other functions (whoami(), triggerResolverRun(), uploadSingle(), getRunStatus()), the legacy ChalkClient should be used (but ideally only for these four calls - we still strongly recommend partially migrating the query calls to the gRPC Client).

Supported environment variables

VariableKindDescription
process.env._CHALK_CLIENT_IDRequiredYour Chalk client ID. You must specify this environment variable or pass an explicit clientId value when constructing your ChalkClient
process.env._CHALK_CLIENT_SECRETRequiredYour Chalk client secret. You must specify this environment variable or pass an explicit clientSecret value when constructing your ChalkClient
process.env._CHALK_ACTIVE_ENVIRONMENTOptionalThe environment that your client should connect to. If not specified, the client will query your project's default environment
process.env._CHALK_API_SERVEROptionalThe API server that the client will communicate with. This defaults to https://api.chalk.ai which should be sufficient for most consumers

You can find relevant variables to use with your Chalk Client by running chalk config with the Chalk command line tool.

$ chalk config

Path:          ~~~
Name:          Organization Token
Client Id:     <client-id>
Client Secret: <client-secret>
Environment:   <active-environment>
API Server:    https://api.chalk.ai
Valid Until:   2023-11-10T06:11:17.516000

Constructor options (Legacy)

import { ChalkCLient } from "@chalk-ai/client"

const options: ChalkClientOpts = {/* ... */};
const chalkClient = new ChalkClient(opts);

export interface ChalkClientOpts {
  /**
   * Your Chalk Client ID. This value will be read from the _CHALK_CLIENT_ID environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientId?: string;

  /**
   * Your Chalk Client Secret. This value will be read from the _CHALK_CLIENT_SECRET environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientSecret?: string;

  /**
   * The URL of your chalk API server. Defaults to https://api.chalk.ai
   */
  apiServer?: string;

  /**
   * The environment that your client will run against. This value will be read from the _CHALK_ACTIVE_ENVIRONMENT environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  activeEnvironment?: string;

  /**
   * A custom fetch client that will replace the fetch polyfill used by default.
   *
   * If not provided, the client will use the default fetch polyfill (native fetch with node-fetch as a fallback).
   */
  fetch?: CustomFetchClient;

  /**
   * A custom fetch headers object that will replace the fetch Headers polyfill used by default.
   *
   * If not provided, the client will use the default fetch Headers polyfill (native fetch with node-fetch as a fallback).
   */
  fetchHeaders?: typeof Headers;

  /**
   * The format to use for date-type data.
   *
   * Defaults to "ISO_8601" (in UTC), also supports "EPOCH_MILLIS" as number of milliseconds since epoch
   */
  timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;

  /**
   * If true, will try to directly connect to the query client using the metadata provided during
   * credentials exchange, which returns a mapping between environment and query server.
   *
   * The queryServer option will take precedence over this option (i.e. this option is essentially useless).
   *
   * Defaults to false, the legacy behavior of this client. This will change at the next major release.
   */
  useQueryServerFromCredentialExchange?: boolean;
}

版本与发布记录

@chalk-ai/client 共发布 47 个版本,首次发布于 2022/11/11;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.31.13latest2026/08/063 个月前
1.31.122026/03/108 个月前
1.31.82025/07/311.3 年前
1.31.72025/07/251.3 年前
1.31.62025/07/231.3 年前
1.31.42025/07/111.3 年前

关于 @chalk-ai/client 的常见问题

@chalk-ai/client 是什么?

TypeScript client for Chalk

如何安装 @chalk-ai/client?

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

@chalk-ai/client 支持 TypeScript 吗?

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

@chalk-ai/client 支持 ESM 还是 CommonJS?

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

@chalk-ai/client 可以商用吗?

@chalk-ai/client 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@chalk-ai/client 的下载量有多少?

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

@chalk-ai/client 还在维护吗?

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