RepoCloudflare (Workers AI)Cloudflare (Workers AI)published Feb 5, 2024seen 5d

cloudflare/cloudflare-typescript

TypeScript

Open original ↗

Captured source

source ↗

cloudflare/cloudflare-typescript

Description: The official TypeScript library for the Cloudflare API

Language: TypeScript

License: Apache-2.0

Stars: 747

Forks: 149

Open issues: 24

Created: 2024-02-05T19:50:33Z

Pushed: 2026-06-10T21:40:49Z

Default branch: main

Fork: no

Archived: no

README:

Cloudflare TypeScript API Library

This library provides convenient access to the Cloudflare REST API from server-side TypeScript or JavaScript.

The REST API documentation can be found on developers.cloudflare.com. The full API of this library can be found in [api.md](api.md).

MCP Server

Use the Cloudflare MCP Server to enable AI assistants to interact with this API, allowing them to explore endpoints, make test requests, and use documentation to help integrate this SDK into your application.

![Add to Cursor](https://cursor.com/en-US/install-mcp?name=cloudflare-mcp&config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsImNsb3VkZmxhcmUtbWNwIl0sImVudiI6eyJDTE9VREZMQVJFX0FQSV9UT0tFTiI6IlNuM2xaSlRCWDZra2c3T2RjQlVBeE9POTYzR0VJeUdRcW5GVE9GWVkiLCJDTE9VREZMQVJFX0FQSV9LRVkiOiIxNDRjOWRlZmFjMDQ5NjljN2JmYWQ4ZWZhYThlYTE5NCIsIkNMT1VERkxBUkVfRU1BSUwiOiJ1c2VyQGV4YW1wbGUuY29tIiwiQ0xPVURGTEFSRV9BUElfVVNFUl9TRVJWSUNFX0tFWSI6InYxLjAtMTQ0YzlkZWZhYzA0OTY5YzdiZmFkOGVmLTYzMWE0MWQwMDNhMzJkMjVmZTg3ODA4MWVmMzY1YzQ5NTAzZjdmYWRhNjAwZGE5MzVlMjg1MWExYzczMjYwODRiODVjYmY2NDI5YzRiODU5ZGU4NDc1NzMxZGM5MmE5YzMyOTYzMWU2ZDU5ZTZjNzNkYTdiMTk4NDk3MTcyYjRjZWZlMDcxZDkwZDBmNWQyNzE5In19)

> Note: You may need to set environment variables in your MCP client.

Installation

npm install cloudflare

Usage

The full API of this library can be found in [api.md](api.md).

import Cloudflare from 'cloudflare';

const client = new Cloudflare({
apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted
});

const zone = await client.zones.create({
account: { id: '023e105f4ecef8ad9ca31a8372d0c353' },
name: 'example.com',
type: 'full',
});

console.log(zone.id);

Request & Response types

This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:

import Cloudflare from 'cloudflare';

const client = new Cloudflare({
apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted
});

const params: Cloudflare.ZoneCreateParams = {
account: { id: '023e105f4ecef8ad9ca31a8372d0c353' },
name: 'example.com',
type: 'full',
};
const zone: Cloudflare.Zone = await client.zones.create(params);

Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.

File uploads

Request parameters that correspond to file uploads can be passed in many different forms:

  • File (or an object with the same structure)
  • a fetch Response (or an object with the same structure)
  • an fs.ReadStream
  • the return value of our toFile helper
import fs from 'fs';
import fetch from 'node-fetch';
import Cloudflare, { toFile } from 'cloudflare';

const client = new Cloudflare();

// If you have access to Node `fs` we recommend using `fs.createReadStream()`:
await client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: fs.createReadStream('/path/to/file'),
});

// Or if you have the web `File` API you can pass a `File` instance:
await client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: new File(['my bytes'], 'file'),
});

// You can also pass a `fetch` `Response`:
await client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: await fetch('https://somesite/file'),
});

// Finally, if none of the above are convenient, you can use our `toFile` helper:
await client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: await toFile(Buffer.from('my bytes'), 'file'),
});
await client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: await toFile(new Uint8Array([0, 1, 2]), 'file'),
});

Handling errors

When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of APIError will be thrown:

const zone = await client.zones
.get({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' })
.catch(async (err) => {
if (err instanceof Cloudflare.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});

Error codes are as follows:

| Status Code | Error Type | | ----------- | -------------------------- | | 400 | BadRequestError | | 401 | AuthenticationError | | 403 | PermissionDeniedError | | 404 | NotFoundError | | 422 | UnprocessableEntityError | | 429 | RateLimitError | | >=500 | InternalServerError | | N/A | APIConnectionError |

Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.

You can use the maxRetries option to configure or disable this:

// Configure the default for all requests:
const client = new Cloudflare({
maxRetries: 0, // default is 2
});

// Or, configure per-request:
await client.zones.get({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' }, {
maxRetries: 5,
});

Timeouts

Requests time out after 1 minute by default. You can configure this with a timeout option:

// Configure the default for all requests:
const client = new Cloudflare({
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
});

// Override per-request:
await client.zones.edit({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' }, {
timeout: 5 * 1000,
});

On timeout, an APIConnectionTimeoutError is thrown.

Note that requests which time out will be [retried twice by default](#retries).

Auto-pagination

List methods in the Cloudflare API are paginated. You can use the for await … of

Excerpt shown — open the source for the full document.