@authzed/authzed-node

中文文档
1.6.1发布于 2025/10/07Apache-2.0TypeScriptESM + CJS近 30 天下载 188.9万

authzed client for nodejs

原始使用文档
文档目录8 节
  1. Getting Started
  2. Basic Usage
  3. Installation
  4. Initializing a client
  5. Performing an API call
  6. Promises (async/await) support
  7. Writing Caveats
  8. Requirements

Authzed NodeJS Client

npm version License Build Status Mailing List Discord Server Twitter

This repository houses the NodeJS client library for Authzed.

Authzed is a database and service that stores, computes, and validates your application's permissions.

Developers create a schema that models their permissions requirements and use a client library, such as this one, to apply the schema to the database, insert data into the database, and query the data to efficiently check permissions in their applications.

Supported client API versions:

You can find more info on each API on the Authzed API reference documentation. Additionally, Protobuf API documentation can be found on the Buf Registry Authzed API repository.

See CONTRIBUTING.md for instructions on how to contribute and perform common tasks like building the project and running tests.

Getting Started

We highly recommend following the Protecting Your First App guide to learn the latest best practice to integrate an application with Authzed.

If you're interested in examples of a specific version of the API, they can be found in their respective folders in the examples directory.

Basic Usage

Installation

The project is packaged and distributed via NPM.

If you are using the typical npm toolchain, the command to install the library is:

npm i @authzed/authzed-node

Initializing a client

Everything required to connect and make API calls is located in a module respective to API version.

You will have to provide a your own API Token from the [Authzed dashboard] in place of t_your_token_here_1234567deadbeef in the following example:

import { v1 } from '@authzed/authzed-node';
// if your endpoint is localhost
// const client = v1.NewClient('t_your_token_here_1234567deadbeef', 'localhost:50051', v1.ClientSecurity.INSECURE_LOCALHOST_ALLOWED);
const client = v1.NewClient('t_your_token_here_1234567deadbeef');

Or to use a custom certificate authority, load the CA certificate and pass the file reference to NewClientWithCustomCert.

import { v1 } from '@authzed/authzed-node';
import fs from 'fs';

const endpoint = 'localhost:50051';
const cert = fs.readFileSync('path/to/cert.pem');
const client = v1.NewClientWithCustomCert('t_your_token_here_1234567deadbeef', endpoint, cert);

Performing an API call

Because of the verbosity of these types, we recommend writing your own functions/methods to create these types from your existing application's models.

The create method on generated classes takes attributes as input and defaults unspecified attributes to their empty value. This allows you to create request messages, for example, by specifying only relevant fields and leaves optional fields empty.

import { v1 } from '@authzed/authzed-node';

const client = v1.NewClient('token')

// Create the relationship between the resource and the user.
const firstPost = v1.ObjectReference.create({
    objectType: "blog/post",
    objectId: "1",
});

// Create the user reference.
const emilia = v1.ObjectReference.create({
    objectType: "blog/user",
    objectId: "emilia",
});

// Create the subject reference using the user reference
const subject = v1.SubjectReference.create({
    object: emilia,
});

const checkPermissionRequest = v1.CheckPermissionRequest.create({
    resource: firstPost,
    permission: "read",
    subject,
});

client.checkPermission(checkPermissionRequest, (err, response) => {
    console.log(response);
    console.log(err);
});

Promises (async/await) support

Each method available in the client has an associated promise-style method in place of callbacks, that can be accessed at the .promises property on the client.

import { v1 } from '@authzed/authzed-node';

const client = v1.NewClient('token');
const { promises: promiseClient } = client; // access client.promises

const checkPermissionRequest = /** from above **/;

const result = await promiseClient.checkPermission(checkPermissionRequest);

For stream-returning methods, including client.readRelationships(), client.lookupResources() and client.lookupSubjects(), the promise-style method will result in an array of response objects once the stream has been closed.

import { v1 } from '@authzed/authzed-node';

const client = v1.NewClient('token');
const { promises: promiseClient } = client; // access client.promises

const results = await promiseClient.readRelationships(/** req **/);
console.log(results[0]); // first ReadRelationship result

Writing Caveats

When writing caveat context to a relation, we've observed that you need to use the Struct object bundled with authzed-node, rather than importing it from the google-protobuf package. If you don't use the authzed-node version, writing context will fail silently and the relations won't reflect the context.

An example:

import { protobuf } from '@authzed/authzed-node';
const { Struct } = protobuf;

const writeRequest = WriteRelationshipsRequest.create({
updates: [
  RelationshipUpdate.create({
    relationship: Relationship.create({
      resource: resource,
      relation: "caveated_viewer",
      subject: SubjectReference.create({
        object: testUser,
      }),
      optionalCaveat: ContextualizedCaveat.create({
        caveatName: "has_special_attribute",
        context: Struct.fromJson({
          special: true,
        }),
      }),
    }),
    operation: RelationshipUpdate_Operation.CREATE,
  }),
],
});

const response = await client.writeRelationships(writeRequest);

Requirements

Supported Node.js versions: 18, 20, 22

Minimum TypeScript version 3.8

版本与发布记录

@authzed/authzed-node 共发布 39 个版本,首次发布于 2021/07/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.6.1latest2025/10/071.1 年前
1.6.02025/10/021.1 年前
1.5.12025/06/261.4 年前
1.5.02025/06/161.4 年前
1.4.12025/06/121.4 年前
1.4.02025/06/101.4 年前

关于 @authzed/authzed-node 的常见问题

@authzed/authzed-node 是什么?

authzed client for nodejs

如何安装 @authzed/authzed-node?

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

@authzed/authzed-node 支持 TypeScript 吗?

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

@authzed/authzed-node 支持 ESM 还是 CommonJS?

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

@authzed/authzed-node 可以商用吗?

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

@authzed/authzed-node 的下载量有多少?

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

@authzed/authzed-node 还在维护吗?

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