@clevercloud/client

中文文档
12.6.11发布于 2026/11/06Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 4.3万

JavaScript REST client and utils for Clever Cloud's API

原始使用文档
文档目录11 节
  1. What is this?
  2. How do I use this?
  3. How can I get the oAuth configuration?
  4. Logs stream and event API stream (v2)
  5. Retrieving logs from Clever Cloud
  6. Retrieving events from Clever Cloud
  7. Handling errors
  8. Close the stream
  9. Auto retry behaviour
  10. Logs stream (v4)
  11. License

JavaScript REST client and utils for Clever Cloud's API

🚧 New Client Documentation Available: For the new command-pattern client (beta), see NEW_CLIENT.md

What is this?

This project contains a REST client for Clever Cloud's API and some utils.

How do I use this?

First, you need to install the Node.js module:

pnpm install @clevercloud/client

We recommend using mise to make sure you rely on the same Node.js & pnpm versions as us (defined in mise.toml).

All API REST paths are accessible as "functions" organized in "services". A call to a function of a service will just prepare the HTTP request and return the request params in an object via a promise. It won't authenticate the request and it won't send it.

You will need to chain some helpers to the function call to:

  • Specify the API host (prod, preprod...)
  • Add the right authentication headers (oAuth v1 or other...)
  • Send the HTTP request with your favourite lib/code
  • Handle the response with your favourite lib/code

Those helpers are ready to use for browsers or Node.js and for oAuth v1 signature auth. The recommended way is to wrap those helpers in one place of your app and to reuse it everywhere you need to send API calls.

Here's an example using ECMAScript modules with oAuth v1 signature and using the fetch API to send requests.

In a file, expose this function:

import { addOauthHeader } from '@clevercloud/client/esm/oauth.js';
import { prefixUrl } from '@clevercloud/client/esm/prefix-url.js';
import { request } from '@clevercloud/client/esm/request.fetch.js';

export function sendToApi (requestParams) {

  // load and cache config and tokens
  const API_HOST = 'https://api.clever-cloud.com'
  const tokens = {
    OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY',
    OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET',
    API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN',
    API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET',
  }

  return Promise.resolve(requestParams)
    .then(prefixUrl(API_HOST))
    .then(addOauthHeader(tokens))
    .then(request);
    // chain a .catch() call here if you need to handle some errors or maybe redirect to login
}

NOTE: If your project relies on a specific REST library (axios, jQuery...), you'll have to write your own request function to plug the params to your lib instead of using request.fetch.js.

Then, in any file of your app, import the API function you need directly (it helps with tree shaking), call it and add a .then(sendToApi) like this:

import { getAllEnvVars } from '@clevercloud/client/esm/api/v2/application.js';
import { sendToApi } from '../send-to-api.js';

const envVars = await getAllEnvVars({ id: oid, appId }).then(sendToApi);

NOTE: It returns a promise, you may want to use await with it.

How can I get the oAuth configuration?

A general documentation is proposed on our Website. As stated in the documentation:

You need to create an oAuth consumer token in the Clever Cloud console. Click on "Create...", then on "an oauth consumer" under your organization name. All created consumers will appear under the list of applications and add-ons.

Once you got the consumer, you still need to generate the aAuth tokens. You may do the whole aAuth dance in the browser.

If you use the clever-tools CLI, you can also generate tokens using the following command :

clever login

Once successfully logged in, you’ll be provided with a token / secret couple.

Logs stream and event API stream (v2)

Retrieving logs from Clever Cloud

This stream is exposed the inner SSE source with component-emitter, a tiny lib implementing an API that closely match both EventTarget from the DOM and EventEmitter from Node.js.

Here's an example of how to use LogsStream to retrieve live logs from an app:

// Browser implementation or Node.js implementation
import { LogsStream } from '@clevercloud/client/esm/streams/logs.browser.js';
// import { LogsStream } from '@clevercloud/client/esm/streams/logs.node.js';

// Load and cache config and tokens
const API_HOST = 'https://api.clever-cloud.com';
const tokens = {
  OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY',
  OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET',
  API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN',
  API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET',
};

// Create a LogsStream instance (filter and deploymentId are optional)
const logsStream = new LogsStream({ apiHost: API_HOST, tokens, appId, filter, deploymentId });

// Listen to "log" events
logsStream.on('log', (rawLogLine) => console.log(rawLogLine));

// Open the stream
logsStream.open();

Retrieving events from Clever Cloud

This stream is exposed the inner WebSocket source with component-emitter, a tiny lib implementing an API that closely match both EventTarget from the DOM and EventEmitter from Node.js.

Here's an example of how to use EventsStream to retrieve events from the Clever Cloud platform:

// Browser and Node.js 21+ implementation
import { EventsStream } from '@clevercloud/client/esm/streams/events.js';

// Load and cache config and tokens
const API_HOST = 'https://api.clever-cloud.com';
const tokens = {
  OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY',
  OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET',
  API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN',
  API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET',
};

// Create an EventsStream instance (appId is optional)
const eventsStream = new EventsStream({ apiHost: API_HOST, tokens, appId });

// Listen to "event" events
eventsStream.on('event', (rawEvent) => console.log(rawEvent));

// Open the stream
eventsStream.open();

Handling errors

When an error (network failures, bad authentication...) occurs with the source stream, an error event is emitted:

stream.on('error', (error) => console.error(error));
  • By default, when a network failure is detected, an error is emitted and the stream is closed automatically.
  • If you enable the auto retry behaviour, when a network failure is detected, it won't emit an error.
  • If you enable the auto retry behaviour AND set a max number of retries, it will emit an error.

See "auto retry behaviour" section for more details...

Close the stream

You can close the stream at any time like this:

stream.close();

It won't remove event listeners from the stream instance so you can re-open it easily.

Auto retry behaviour

When you call .open() on a stream, it's not resilient to network failures by default. We have an opt-in auto retry behaviour to handle those, you can enable it when you open the stream like this:

stream.open({ autoRetry: true });

Network failure are detected with a ping/pong system. When such a failure is detected and auto retry behaviour is enabled, the source stream is closed and a new open is attempted automatically.

You probably don't need to but you can listen to events related to this auto retry behaviour:

// "close" event is emitted each time a network failure is detected (or any unknown error not related to authentication)
stream.on('close', (reason) => console.log('Stream closed because of', reason));
// "open" event is emitted on first .open() call and each time a new open is attempted
stream.on('open', () => console.log('Stream open...'));
// "ping" event is emitted each time a ping is received from the source stream
stream.on('ping', () => console.log('Received ping'));

When enabled, the auto retry behaviour will follow an infinite exponential backoff pattern. You can change the default timings and max number of retries like this:

stream.open({
  autoRetry: true,
  // Factor used to compute exponential backoff delays, defaults to 1.25
  backoffFactor: 1.5,
  // First iteration timeout in ms, also used to compute exponential backoff delays, defaults to 1500
  initRetryTimeout: 2000,
  // Maximum number of consecutive iterations the auto retry behaviour can do, defaults to Infinity
  maxRetryCount: 6,
});

Logs stream (v4)

import { ApplicationLogStream } from '@clevercloud/client/esm/streams/application-log.js';

// Load and cache config and tokens
const API_HOST = 'https://api.clever-cloud.com';
const tokens = {
  OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY',
  OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET',
  API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN',
  API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET',
};

// Create an EventsStream instance (appId is optional)
const logsStream = new ApplicationLogStream({
  apiHost: API_HOST,
  tokens,
  ownerId: 'YYY',
  appId: 'XXX',
  // optionnal auto retry config
  // retryConfiguration: {
  //   enabled: true,
    // backoffFactor: number,
    // initRetryTimeout: number,
    // maxRetryCount: number,
  // },
  // optionnal logs request params
  // since: Date,
  // until: Date,
  // service: string[],
  // limit: number,
  // deploymentId: string,
  // instanceId: string[],
  // filter: string,
  // field: string[],
  // throttleElements: number,
  // throttlePerInMilliseconds: number,
});

logsStream
  .on('open', (event) => console.debug('stream opened!', event))
  .on('error', (event) => console.error('error', event.error))
  .onLog((event) => console.log(event.data.date, event.data.message));

logsStream.start();

// You can also pause the stream
logsStream.pause();

// And resume it
logsStream.resume();

License

This project is licensed under the Apache-2.0.

版本与发布记录

@clevercloud/client 共发布 97 个版本,首次发布于 2019/07/23;过去 12 个月发布了 25 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
12.6.11latest2026/11/063 天前
12.6.102026/11/046 天前
12.6.92026/11/046 天前
12.6.82026/11/027 天前
12.6.72026/10/2218 天前
12.6.62026/10/011 个月前

其他发布通道:beta → 7.7.0-beta.0。

关于 @clevercloud/client 的常见问题

@clevercloud/client 是什么?

JavaScript REST client and utils for Clever Cloud's API

如何安装 @clevercloud/client?

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

@clevercloud/client 支持 TypeScript 吗?

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

@clevercloud/client 支持 ESM 还是 CommonJS?

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

@clevercloud/client 需要什么版本的 Node.js?

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

@clevercloud/client 可以商用吗?

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

@clevercloud/client 的下载量有多少?

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

@clevercloud/client 还在维护吗?

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