{"id":15715421,"url":"https://github.com/cloudflare/cloudflare-typescript","last_synced_at":"2026-04-16T00:04:26.855Z","repository":{"id":227368018,"uuid":"753272060","full_name":"cloudflare/cloudflare-typescript","owner":"cloudflare","description":"The official Typescript library for the Cloudflare API","archived":false,"fork":false,"pushed_at":"2025-05-08T04:20:15.000Z","size":17304,"stargazers_count":431,"open_issues_count":11,"forks_count":51,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-05-12T06:38:31.924Z","etag":null,"topics":["api","cloudflare","sdk","sdk-typescript"],"latest_commit_sha":null,"homepage":"https://developers.cloudflare.com/api","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloudflare.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-02-05T19:50:33.000Z","updated_at":"2025-05-10T20:43:44.000Z","dependencies_parsed_at":"2024-04-17T00:27:31.584Z","dependency_job_id":"ba5f31c9-2558-435b-91c5-d4c9b6f81055","html_url":"https://github.com/cloudflare/cloudflare-typescript","commit_stats":{"total_commits":1400,"total_committers":7,"mean_commits":200.0,"dds":0.2142857142857143,"last_synced_commit":"4d834f1d6c3d87b6bdcdec6d1e9b38a663c7249a"},"previous_names":["cloudflare/cloudflare-typescript"],"tags_count":62,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fcloudflare-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fcloudflare-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fcloudflare-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudflare%2Fcloudflare-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudflare","download_url":"https://codeload.github.com/cloudflare/cloudflare-typescript/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253950608,"owners_count":21989379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","cloudflare","sdk","sdk-typescript"],"created_at":"2024-10-03T21:41:31.326Z","updated_at":"2026-04-16T00:04:26.847Z","avatar_url":"https://github.com/cloudflare.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Core Platform \u0026 SDKs"],"sub_categories":["Official SDKs"],"readme":"# Cloudflare TypeScript API Library\n\n[![NPM version](https://img.shields.io/npm/v/cloudflare.svg)](https://npmjs.org/package/cloudflare) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/cloudflare)\n\nThis library provides convenient access to the Cloudflare REST API from server-side TypeScript or JavaScript.\n\nThe REST API documentation can be found on [developers.cloudflare.com](https://developers.cloudflare.com/api). The full API of this library can be found in [api.md](api.md).\n\n## MCP Server\n\nUse 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.\n\n[![Add to Cursor](https://cursor.com/deeplink/mcp-install-dark.svg)](https://cursor.com/en-US/install-mcp?name=cloudflare-mcp\u0026config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsImNsb3VkZmxhcmUtbWNwIl19)\n[![Install in VS Code](https://img.shields.io/badge/_-Add_to_VS_Code-blue?style=for-the-badge\u0026logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGZpbGw9Im5vbmUiIHZpZXdCb3g9IjAgMCA0MCA0MCI+PHBhdGggZmlsbD0iI0VFRSIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMzAuMjM1IDM5Ljg4NGEyLjQ5MSAyLjQ5MSAwIDAgMS0xLjc4MS0uNzNMMTIuNyAyNC43OGwtMy40NiAyLjYyNC0zLjQwNiAyLjU4MmExLjY2NSAxLjY2NSAwIDAgMS0xLjA4Mi4zMzggMS42NjQgMS42NjQgMCAwIDEtMS4wNDYtLjQzMWwtMi4yLTJhMS42NjYgMS42NjYgMCAwIDEgMC0yLjQ2M0w3LjQ1OCAyMCA0LjY3IDE3LjQ1MyAxLjUwNyAxNC41N2ExLjY2NSAxLjY2NSAwIDAgMSAwLTIuNDYzbDIuMi0yYTEuNjY1IDEuNjY1IDAgMCAxIDIuMTMtLjA5N2w2Ljg2MyA1LjIwOUwyOC40NTIuODQ0YTIuNDg4IDIuNDg4IDAgMCAxIDEuODQxLS43MjljLjM1MS4wMDkuNjk5LjA5MSAxLjAxOS4yNDVsOC4yMzYgMy45NjFhMi41IDIuNSAwIDAgMSAxLjQxNSAyLjI1M3YuMDk5LS4wNDVWMzMuMzd2LS4wNDUuMDk1YTIuNTAxIDIuNTAxIDAgMCAxLTEuNDE2IDIuMjU3bC04LjIzNSAzLjk2MWEyLjQ5MiAyLjQ5MiAwIDAgMS0xLjA3Ny4yNDZabS43MTYtMjguOTQ3LTExLjk0OCA5LjA2MiAxMS45NTIgOS4wNjUtLjAwNC0xOC4xMjdaIi8+PC9zdmc+)](https://vscode.stainless.com/mcp/%7B%22name%22%3A%22cloudflare-mcp%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22cloudflare-mcp%22%5D%7D)\n\n\u003e Note: You may need to set environment variables in your MCP client.\n\n## Installation\n\n```sh\nnpm install cloudflare\n```\n\n## Usage\n\nThe full API of this library can be found in [api.md](api.md).\n\n\u003c!-- prettier-ignore --\u003e\n```js\nimport Cloudflare from 'cloudflare';\n\nconst client = new Cloudflare({\n  apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted\n});\n\nconst zone = await client.zones.create({\n  account: { id: '023e105f4ecef8ad9ca31a8372d0c353' },\n  name: 'example.com',\n  type: 'full',\n});\n\nconsole.log(zone.id);\n```\n\n### Request \u0026 Response types\n\nThis library includes TypeScript definitions for all request params and response fields. You may import and use them like so:\n\n\u003c!-- prettier-ignore --\u003e\n```ts\nimport Cloudflare from 'cloudflare';\n\nconst client = new Cloudflare({\n  apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted\n});\n\nconst params: Cloudflare.ZoneCreateParams = {\n  account: { id: '023e105f4ecef8ad9ca31a8372d0c353' },\n  name: 'example.com',\n  type: 'full',\n};\nconst zone: Cloudflare.Zone = await client.zones.create(params);\n```\n\nDocumentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.\n\n## File uploads\n\nRequest parameters that correspond to file uploads can be passed in many different forms:\n\n- `File` (or an object with the same structure)\n- a `fetch` `Response` (or an object with the same structure)\n- an `fs.ReadStream`\n- the return value of our `toFile` helper\n\n```ts\nimport fs from 'fs';\nimport fetch from 'node-fetch';\nimport Cloudflare, { toFile } from 'cloudflare';\n\nconst client = new Cloudflare();\n\n// If you have access to Node `fs` we recommend using `fs.createReadStream()`:\nawait client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {\n  account_id: '023e105f4ecef8ad9ca31a8372d0c353',\n  value: fs.createReadStream('/path/to/file'),\n});\n\n// Or if you have the web `File` API you can pass a `File` instance:\nawait client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {\n  account_id: '023e105f4ecef8ad9ca31a8372d0c353',\n  value: new File(['my bytes'], 'file'),\n});\n\n// You can also pass a `fetch` `Response`:\nawait client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {\n  account_id: '023e105f4ecef8ad9ca31a8372d0c353',\n  value: await fetch('https://somesite/file'),\n});\n\n// Finally, if none of the above are convenient, you can use our `toFile` helper:\nawait client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {\n  account_id: '023e105f4ecef8ad9ca31a8372d0c353',\n  value: await toFile(Buffer.from('my bytes'), 'file'),\n});\nawait client.kv.namespaces.values.update('0f2ac74b498b48028cb68387c421e279', 'My-Key', {\n  account_id: '023e105f4ecef8ad9ca31a8372d0c353',\n  value: await toFile(new Uint8Array([0, 1, 2]), 'file'),\n});\n```\n\n## Handling errors\n\nWhen the library is unable to connect to the API,\nor if the API returns a non-success status code (i.e., 4xx or 5xx response),\na subclass of `APIError` will be thrown:\n\n\u003c!-- prettier-ignore --\u003e\n```ts\nconst zone = await client.zones\n  .get({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' })\n  .catch(async (err) =\u003e {\n    if (err instanceof Cloudflare.APIError) {\n      console.log(err.status); // 400\n      console.log(err.name); // BadRequestError\n      console.log(err.headers); // {server: 'nginx', ...}\n    } else {\n      throw err;\n    }\n  });\n```\n\nError codes are as follows:\n\n| Status Code | Error Type                 |\n| ----------- | -------------------------- |\n| 400         | `BadRequestError`          |\n| 401         | `AuthenticationError`      |\n| 403         | `PermissionDeniedError`    |\n| 404         | `NotFoundError`            |\n| 422         | `UnprocessableEntityError` |\n| 429         | `RateLimitError`           |\n| \u003e=500       | `InternalServerError`      |\n| N/A         | `APIConnectionError`       |\n\n### Retries\n\nCertain errors will be automatically retried 2 times by default, with a short exponential backoff.\nConnection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,\n429 Rate Limit, and \u003e=500 Internal errors will all be retried by default.\n\nYou can use the `maxRetries` option to configure or disable this:\n\n\u003c!-- prettier-ignore --\u003e\n```js\n// Configure the default for all requests:\nconst client = new Cloudflare({\n  maxRetries: 0, // default is 2\n});\n\n// Or, configure per-request:\nawait client.zones.get({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' }, {\n  maxRetries: 5,\n});\n```\n\n### Timeouts\n\nRequests time out after 1 minute by default. You can configure this with a `timeout` option:\n\n\u003c!-- prettier-ignore --\u003e\n```ts\n// Configure the default for all requests:\nconst client = new Cloudflare({\n  timeout: 20 * 1000, // 20 seconds (default is 1 minute)\n});\n\n// Override per-request:\nawait client.zones.edit({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' }, {\n  timeout: 5 * 1000,\n});\n```\n\nOn timeout, an `APIConnectionTimeoutError` is thrown.\n\nNote that requests which time out will be [retried twice by default](#retries).\n\n## Auto-pagination\n\nList methods in the Cloudflare API are paginated.\nYou can use the `for await … of` syntax to iterate through items across all pages:\n\n```ts\nasync function fetchAllAccounts(params) {\n  const allAccounts = [];\n  // Automatically fetches more pages as needed.\n  for await (const account of client.accounts.list()) {\n    allAccounts.push(account);\n  }\n  return allAccounts;\n}\n```\n\nAlternatively, you can request a single page at a time:\n\n```ts\nlet page = await client.accounts.list();\nfor (const account of page.result) {\n  console.log(account);\n}\n\n// Convenience methods are provided for manually paginating:\nwhile (page.hasNextPage()) {\n  page = await page.getNextPage();\n  // ...\n}\n```\n\n## Advanced Usage\n\n### Accessing raw Response data (e.g., headers)\n\nThe \"raw\" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.\n\nYou can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.\n\n\u003c!-- prettier-ignore --\u003e\n```ts\nconst client = new Cloudflare();\n\nconst response = await client.zones\n  .create({\n    account: { id: '023e105f4ecef8ad9ca31a8372d0c353' },\n    name: 'example.com',\n    type: 'full',\n  })\n  .asResponse();\nconsole.log(response.headers.get('X-My-Header'));\nconsole.log(response.statusText); // access the underlying Response object\n\nconst { data: zone, response: raw } = await client.zones\n  .create({\n    account: { id: '023e105f4ecef8ad9ca31a8372d0c353' },\n    name: 'example.com',\n    type: 'full',\n  })\n  .withResponse();\nconsole.log(raw.headers.get('X-My-Header'));\nconsole.log(zone.id);\n```\n\n### Making custom/undocumented requests\n\nThis library is typed for convenient access to the documented API. If you need to access undocumented\nendpoints, params, or response properties, the library can still be used.\n\n#### Undocumented endpoints\n\nTo make requests to undocumented endpoints, you can use `client.get`, `client.post`, and other HTTP verbs.\nOptions on the client, such as retries, will be respected when making these requests.\n\n```ts\nawait client.post('/some/path', {\n  body: { some_prop: 'foo' },\n  query: { some_query_arg: 'bar' },\n});\n```\n\n#### Undocumented request params\n\nTo make requests using undocumented parameters, you may use `// @ts-expect-error` on the undocumented\nparameter. This library doesn't validate at runtime that the request matches the type, so any extra values you\nsend will be sent as-is.\n\n```ts\nclient.foo.create({\n  foo: 'my_param',\n  bar: 12,\n  // @ts-expect-error baz is not yet public\n  baz: 'undocumented option',\n});\n```\n\nFor requests with the `GET` verb, any extra params will be in the query, all other requests will send the\nextra param in the body.\n\nIf you want to explicitly send an extra argument, you can do so with the `query`, `body`, and `headers` request\noptions.\n\n#### Undocumented response properties\n\nTo access undocumented response properties, you may access the response object with `// @ts-expect-error` on\nthe response object, or cast the response object to the requisite type. Like the request params, we do not\nvalidate or strip extra properties from the response from the API.\n\n### Customizing the fetch client\n\nBy default, this library uses `node-fetch` in Node, and expects a global `fetch` function in other environments.\n\nIf you would prefer to use a global, web-standards-compliant `fetch` function even in a Node environment,\n(for example, if you are running Node with `--experimental-fetch` or using NextJS which polyfills with `undici`),\nadd the following import before your first import `from \"Cloudflare\"`:\n\n```ts\n// Tell TypeScript and the package to use the global web fetch instead of node-fetch.\n// Note, despite the name, this does not add any polyfills, but expects them to be provided if needed.\nimport 'cloudflare/shims/web';\nimport Cloudflare from 'cloudflare';\n```\n\nTo do the inverse, add `import \"cloudflare/shims/node\"` (which does import polyfills).\nThis can also be useful if you are getting the wrong TypeScript types for `Response` ([more details](https://github.com/cloudflare/cloudflare-typescript/tree/main/src/_shims#readme)).\n\n### Logging and middleware\n\nYou may also provide a custom `fetch` function when instantiating the client,\nwhich can be used to inspect or alter the `Request` or `Response` before/after each request:\n\n```ts\nimport { fetch } from 'undici'; // as one example\nimport Cloudflare from 'cloudflare';\n\nconst client = new Cloudflare({\n  fetch: async (url: RequestInfo, init?: RequestInit): Promise\u003cResponse\u003e =\u003e {\n    console.log('About to make a request', url, init);\n    const response = await fetch(url, init);\n    console.log('Got response', response);\n    return response;\n  },\n});\n```\n\nNote that if given a `DEBUG=true` environment variable, this library will log all requests and responses automatically.\nThis is intended for debugging purposes only and may change in the future without notice.\n\n### Configuring an HTTP(S) Agent (e.g., for proxies)\n\nBy default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP \u0026 TLS handshakes and shaving around 100ms off most requests.\n\nIf you would like to disable or customize this behavior, for example to use the API behind a proxy, you can pass an `httpAgent` which is used for all requests (be they http or https), for example:\n\n\u003c!-- prettier-ignore --\u003e\n```ts\nimport http from 'http';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\n\n// Configure the default for all requests:\nconst client = new Cloudflare({\n  httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),\n});\n\n// Override per-request:\nawait client.zones.delete(\n  { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },\n  {\n    httpAgent: new http.Agent({ keepAlive: false }),\n  },\n);\n```\n\n## Semantic versioning\n\nThis package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:\n\n1. Changes that only affect static types, without breaking runtime behavior.\n1. Changes to library internals which are technically public but not intended or documented for external use.\n1. Changes that we do not expect to impact the vast majority of users in practice.\n\n## Requirements\n\nTypeScript \u003e= 4.5 is supported.\n\nThe following runtimes are supported:\n\n- Web browsers (Up-to-date Chrome, Firefox, Safari, Edge, and more)\n- Node.js 18 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.\n- Deno v1.28.0 or higher.\n- Bun 1.0 or later.\n- Cloudflare Workers.\n- Vercel Edge Runtime.\n- Jest 28 or greater with the `\"node\"` environment (`\"jsdom\"` is not supported at this time).\n- Nitro v2.6 or greater.\n\nNote that React Native is not supported at this time.\n\nIf you are interested in other runtime environments, please open or upvote an issue on GitHub.\n\n## Maintenance\n\nThis SDK is actively maintained, however, many issues are tracked outside of GitHub on internal Cloudflare systems. Members of the community are welcome to join and discuss your issues during our twice monthly triage meetings. For urgent issues, please contact [Cloudflare support](https://www.support.cloudflare.com/s/?language=en_US). \n\n* [Community triage meeting](https://calendar.google.com/calendar/embed?src=c_dbf6ce250643f2e60f806d28f3fc09a9de24cbe0ab3ffb699838303d2adfc9e4%40group.calendar.google.com\u0026ctz=America%2FLos_Angeles)\n\n## Contributing\n\nSee [the contributing documentation](./CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudflare%2Fcloudflare-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudflare%2Fcloudflare-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudflare%2Fcloudflare-typescript/lists"}