@auth0/auth0-express-api

中文文档
1.0.0发布于 2026/10/08Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2690

Auth0 SDK for Express API's on JavaScript runtimes

原始使用文档
文档目录10 节
  1. Documentation
  2. Features
  3. Getting Started
  4. 1. Install the SDK
  5. 3. Register the Auth0 Express API router
  6. Feedback
  7. Contributing
  8. Raise an issue
  9. Vulnerability Reporting
  10. What is Auth0?

The Auth0 Express-API SDK is a library for protecting API's in Express applications.

Release Downloads License

📚 Documentation - 🔎 Features - 🚀 Getting Started - 💬 Feedback

Documentation

  • Examples - examples for your different use cases.
  • Docs Site - explore our docs site and learn more about Auth0.

Features

Jump straight to the capability you need.

FeatureWhat it does
Quick startProtect an API with createAuth0Api in a few lines
Environment variablesConfigure from AUTH0_* env vars instead of hardcoding
Protect an API route (requiresAuth)Require a valid bearer access token
Read token claims (req.auth0.user)Access claims extracted from the verified token
Call another API on behalf of the userExchange the caller's token for one issued to a downstream API
Call a third party API (Token Vault)Get a Google or Slack access token for the caller from Token Vault
Exchange an external token (Custom Token Exchange)Turn a legacy or partner token into an Auth0 access token
Handle a failed Token Vault exchangeTell a misconfiguration from a tenant refusal with isConnectionExchangeError
Require specific scopesGate routes with scopesInclude (match all or any)
Authorization with claimsRestrict routes with claimEquals, claimIncludes, claimCheck
Custom token / user typeType your custom claims via module augmentation
Custom fetchSwap in your own fetch (proxies, retries, instrumentation)

Getting Started

1. Install the SDK

npm i @auth0/auth0-express-api

This library requires Node.js 22 LTS and newer LTS versions.

3. Register the Auth0 Express API router

Register the Auth0 Express API router with your Express instance.

import express from 'express';
import { createAuth0Api } from '@auth0/auth0-express-api';

const app = express();

app.use(createAuth0Api({
  domain: '<AUTH0_DOMAIN>',
  audience: '<AUTH0_AUDIENCE>',
}));

The AUTH0_DOMAIN can be obtained from the Auth0 Dashboard once you've created an API. The AUTH0_AUDIENCE is the identifier of the API that is being called. You can find this in the API section of the Auth0 dashboard.

Environment Variable Support: You can also configure the SDK using environment variables (AUTH0_DOMAIN, AUTH0_AUDIENCE). See EXAMPLES.md for details.

Protecting API Routes

In order to protect an API route, you can use the requiresAuth middleware:

import { requiresAuth } from '@auth0/auth0-express-api';

app.get(
  '/protected-api',
  requiresAuth(),
  async (req, res) => {
    res.json({ message: `Hello, ${req.auth0.user!.sub}` });
  }
);

The SDK exposes the claims, extracted from the token, as the user property on the req.auth0 object.

Authorization with Claims

Beyond basic authentication, you can authorize requests based on specific token claims:

import {
  requiresAuth,
  claimEquals,
  claimIncludes,
  claimCheck,
  scopesInclude,
} from '@auth0/auth0-express-api';

// Check if a claim equals a specific value
app.get('/admin', requiresAuth(), claimEquals('isAdmin', true), handler);

// Check if a claim includes all specified values
app.get('/admin/edit', requiresAuth(), claimIncludes('roles', 'admin', 'editor'), handler);

// Custom validation logic
app.get('/premium', requiresAuth(), claimCheck(
  (req, token) => token.tier === 'premium' || token.roles?.includes('admin'),
  'Premium tier or admin role required'
), handler);

// Flexible scope matching - match ALL (default) or ANY
app.get('/admin/edit', requiresAuth(), scopesInclude('read:admin write:admin'), handler);
app.get('/messages', requiresAuth(), scopesInclude('read:messages read:admin', { match: 'any' }), handler);

See EXAMPLES.md for more authorization patterns.

Custom Types

In order to use a custom user type to represent custom claims, you can configure the Token type in a module augmentation:

declare module '@auth0/auth0-express-api' {
  interface Token {
    id: number;
    name: string;
    age: number;
  }
}

Doing so will change the user type on the req.auth0.user object automatically:

app.get(
  '/protected-api',
  requiresAuth(),
  async (req, res) => {
    res.json({ message: `Hello, ${req.auth0.user!.name}` });
  }
);

[!IMPORTANT] The above is to protect API routes by the means of a bearer token, and not server-side rendering routes using a session.

Feedback

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please read the following:

Raise an issue

To provide feedback or report a bug, please raise an issue on our issue tracker.

Vulnerability Reporting

Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

What is Auth0?

Auth0 Logo

Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?

This project is licensed under the Apache License 2.0. See the LICENSE file for more info.

版本与发布记录

@auth0/auth0-express-api 共发布 5 个版本,首次发布于 2026/06/04;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0latest2026/10/0830 天前
1.0.0-beta.3beta2026/08/242 个月前
1.0.0-beta.22026/06/234 个月前
1.0.0-beta.12026/06/194 个月前
1.0.0-beta.02026/06/045 个月前

关于 @auth0/auth0-express-api 的常见问题

@auth0/auth0-express-api 是什么?

Auth0 SDK for Express API's on JavaScript runtimes

如何安装 @auth0/auth0-express-api?

在项目目录运行 npm install @auth0/auth0-express-api,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:express(^4.18.0 || ^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@auth0/auth0-express-api 支持 TypeScript 吗?

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

@auth0/auth0-express-api 支持 ESM 还是 CommonJS?

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

@auth0/auth0-express-api 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.12.0 || >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@auth0/auth0-express-api 可以商用吗?

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

@auth0/auth0-express-api 的下载量有多少?

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

@auth0/auth0-express-api 还在维护吗?

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