Cloudflare API Client
Lightweight universal HTTP client for Cloudflare API
based on Fetch API
that works in Node.js, browser, and CF Workers environment. Optimized for a good
developer experience and minimal code with zero dependencies supporting tree-shaking.
Getting Started
$ npm install cloudflare-client --save
$ yarn add cloudflare-client
Once the library is installed, you can cherry pick and configure individual
Cloudflare API endpoints that you need. If you bundle your code (e.g. with
Rollup), only the selected modules will be included into the application bundle.
import * as Cloudflare from "cloudflare-client";
const dnsRecords = Cloudflare.dnsRecords({
zoneId: "<CLOUDFLARE_ZONE_ID>",
accessToken: "<CLOUDFLARE_API_TOKEN>",
});
const kv = Cloudflare.kv({
accountId: "<CLOUDFLARE_ZONE_ID>",
authKey: "<CLOUDFLARE_AUTH_KEY>",
authEmail: "<CLOUDFLARE_AUTH_EMAIL>",
});
User
const user = Cloudflare.user({ accessToken: "xxx" });
const userDetails = await user.get();
User Tokens
const userTokens = Cloudflare.userTokens({ accessToken: "xxx" });
const token = await userTokens.verify();
const userTokens = Cloudflare.userTokens({ authKey: "xxx", authEmail: "xxx" });
const token = await userTokens.get("ed17574386854bf78a67040be0a770b0");
DNS Records
const dnsRecords = Cloudflare.dnsRecords({ zoneId: "xxx", accessToken: "xxx" });
const records = await dnsRecords.find({ type: "A" }).all();
const record = await dnsRecords.find({ type: "A", name: "test" }).first();
const records = await dnsRecords.find({ type: "A" });
for await (const record of records) {
console.log(record);
}
const record = await dnsRecords.get("372e67954025e0ba6aaa6d586b9e0b59");
const record = await dnsRecords.create({
type: "A",
name: "test.example.com",
content: "192.0.2.1",
proxied: true,
});
const record = await dnsRecords.replace("372e67954025e0ba6aaa6d586b9e0b59", {
type: "A",
name: "test.example.com",
content: "192.0.2.1",
proxied: true,
});
const record = await dnsRecords.update("372e67954025e0ba6aaa6d586b9e0b59", {
proxied: false,
});
await dnsRecords.delete("372e67954025e0ba6aaa6d586b9e0b59");
Workers KV
const kv = Cloudflare.kv({
accountId: "xxx",
authKey: "xxx",
authEmail: "xxx",
});
KV Namespaces
const namespaces = await kv.find().all();
const ns = await kv.create("Example");
await kv.update("0f2ac74b498b48028cb68387c421e279", "New Name");
await kv.delete("0f2ac74b498b48028cb68387c421e279");
Key-Value Pairs
const ns = kv.namespace("0f2ac74b498b48028cb68387c421e279");
const keys = await ns.keys().all();
const keys = await ns.keys({ prefix: "example" }).all();
await ns.set("key", { some: "value" });
const value = await ns.get("key");
await ns.delete("key");
await ns.set("όνομα", "José", { encode: false });
const value = await ns.get("όνομα", { decode: false });
Source Code
For more information and usage examples check out the source code / tests:
Backers 💰
Related Projects
How to Contribute
You're very welcome to create a PR
or send me a message on Discord.
$ git clone https://github.com/kriasoft/cloudflare-client.git
$ cd ./cloudflare-client
$ yarn install
$ yarn test
NOTE: In order to run unit tests locally you will need Node.js v16.15
or newer and Cloudflare API token.
License
Copyright © 2022-present Kriasoft. This source code is licensed under the MIT license found in the
LICENSE file.
Made with ♥ by Konstantin Tarkus (@koistya, blog)
and contributors.