Skip to main content

Quick start

This guide will help you make your first API call with the Cloudflare TypeScript SDK in under 5 minutes.
1

Install the SDK

First, install the Cloudflare SDK using your preferred package manager:
npm install cloudflare
2

Get your API token

You’ll need a Cloudflare API token to authenticate your requests.
  1. Log in to the Cloudflare Dashboard
  2. Go to My Profile > API Tokens
  3. Click Create Token
  4. Use a template or create a custom token with the permissions you need
  5. Copy your token and store it securely
Keep your API token secure! Never commit it to version control or share it publicly.
3

Set up environment variables

Store your API token in an environment variable:
CLOUDFLARE_API_TOKEN=your_api_token_here
4

Make your first API call

Create a new TypeScript or JavaScript file and import the SDK:
import Cloudflare from 'cloudflare';

const client = new Cloudflare({
  apiToken: process.env['CLOUDFLARE_API_TOKEN'],
});

// List all accounts
async function listAccounts() {
  const accounts = await client.accounts.list();
  console.log('Your accounts:');
  for await (const account of accounts) {
    console.log(`- ${account.name} (${account.id})`);
  }
}

listAccounts();
Run your code:
node your-file.js

What’s next?

Create a zone

Learn how to create and manage DNS zones

Configure DNS

Add and update DNS records for your zones

Deploy Workers

Deploy serverless functions at the edge

Error handling

Learn how to handle errors gracefully

Complete example

Here’s a more complete example that creates a zone and adds a DNS record:
import Cloudflare from 'cloudflare';

const client = new Cloudflare({
  apiToken: process.env['CLOUDFLARE_API_TOKEN'],
});

async function setupZone() {
  try {
    // Create a new zone
    const zone = await client.zones.create({
      account: { id: 'YOUR_ACCOUNT_ID' },
      name: 'example.com',
      type: 'full',
    });

    console.log(`Created zone: ${zone.name} (${zone.id})`);

    // Add an A record
    const record = await client.dns.records.create({
      zone_id: zone.id,
      type: 'A',
      name: 'www',
      content: '192.0.2.1',
      ttl: 3600,
      proxied: true,
    });

    console.log(`Created DNS record: ${record.name} -> ${record.content}`);
  } catch (error) {
    if (error instanceof Cloudflare.APIError) {
      console.error('API Error:', error.status, error.message);
    } else {
      console.error('Unexpected error:', error);
    }
  }
}

setupZone();
The SDK provides full TypeScript type definitions. Use your editor’s autocomplete to explore available methods and parameters!

Next steps

Now that you’ve made your first API call, learn more about: