← Back to Blog

How To Programmatically Add DNS Records To Cloudflare In Node

Mastering Cloudflare DNS Management: A Step-by-Step Guide

·Matija Žiberna·
Coding
How To Programmatically Add DNS Records To Cloudflare In Node

Introduction

This guide walks you through the process of integrating Cloudflare DNS with Firebase Functions to manage subdomains dynamically. You will learn how to:

  • Install and configure the Cloudflare SDK
  • Implement DNS record creation, retrieval, listing, and deletion

Prerequisites

Before proceeding, ensure you have:

  • A Cloudflare account with an active domain
  • Cloudflare API credentials (API Key, Email, and Zone ID)
  • Firebase Functions set up in your Node.js project

Navigate to Dashboard and click on a project you want to manage.

For API token navigate to the Cloudflare API Tokens page. Choose Global API Key. cloudflare_account_api

Step 1: Install Cloudflare SDK

To interact with Cloudflare’s API, install the SDK:

npm install cloudflare --save

Step 2: Configure Environment Variables

Store your Cloudflare credentials securely using Firebase environment variables:

export const CLOUDFLARE_API_KEY = functions.params.defineSecret('CLOUDFLARE_API_KEY');
export const CLOUDFLARE_EMAIL = functions.params.defineSecret('CLOUDFLARE_EMAIL');
export const CLOUDFLARE_ZONE_ID = functions.params.defineSecret('CLOUDFLARE_ZONE_ID');
export const ROOT_DOMAIN = functions.params.defineString('ROOT_DOMAIN', { default: 'yourdomain.com' });
export const SITE_BASE_URL = functions.params.defineString('SITE_BASE_URL', { default: 'yourdomain.com' });

Step 3: Cloudflare DNS Management Functions

Cloudflare’s API allows you to dynamically manage DNS records, making it easy to create, retrieve, list, and delete records as needed. This is particularly useful for applications that require automated subdomain management, such as multi-tenant SaaS platforms.

Below, we explore how to interact with Cloudflare DNS using the Node.js SDK, covering essential functions for DNS record management.

Creating a DNS Record

To create a new DNS record (e.g., a CNAME record for a subdomain), use:

await cloudflare.dns.records.create({
  zone_id: zoneId,
  type: 'CNAME',
  name: subdomain,
  content: SITE_BASE_URL.value(),
  ttl: 3600,
  proxied: true,
});

This creates a proxied CNAME record that points the subdomain to your base site URL.

  • zone_id: The unique identifier for your Cloudflare zone (domain).
  • type: The type of DNS record (CNAME, A, MX, etc.).
  • name: The subdomain you want to create (e.g., app.yourdomain.com).
  • content: The target value of the record (e.g., your main website URL).
  • ttl: Time-to-live, which determines how long DNS resolvers should cache the record.
  • proxied: If true, Cloudflare will proxy traffic through its network for performance and security.

Listing All DNS Records

const allRecords = await cloudflare.dns.records.list({
  zone_id: zoneId,
});
console.log(allRecords);

This is useful for auditing and debugging existing DNS configurations.

Retrieving a Specific DNS Record

To fetch a particular record by its name:

const recordName = `${subdomain}.${rootDomain}`;
const records = await cloudflare.dns.records.list({
  zone_id: zoneId,
});
const record = records.result.find((r) => r.name === recordName);
console.log(record);

Deleting a DNS Record

To remove a DNS record dynamically:

const recordToDelete = allRecords.result.find((record) => record.name === subdomain);
if (recordToDelete?.id) {
  await cloudflare.dns.records.delete(recordToDelete.id, { zone_id: zoneId });
}

Adding Different DNS Record Types

Cloudflare allows the creation of various DNS record types beyond CNAME. Here’s how you can create different types:

A Record (Pointing to an IP Address)

await cloudflare.dns.records.create({
  zone_id: zoneId,
  type: 'A',
  name: subdomain,
  content: '192.168.1.1', // Replace with your server's IP
  ttl: 3600,
  proxied: false,
});

TXT Record (For Verification or SPF, DKIM, etc.)

await cloudflare.dns.records.create({
  zone_id: zoneId,
  type: 'TXT',
  name: subdomain,
  content: 'v=spf1 include:_spf.google.com ~all',
  ttl: 3600,
});

MX Record (For Email Routing)

await cloudflare.dns.records.create({
  zone_id: zoneId,
  type: 'MX',
  name: subdomain,
  content: 'mail.yourdomain.com',
  ttl: 3600,
  priority: 10,
});

AAAA Record (For IPv6 Address)

await cloudflare.dns.records.create({
  zone_id: zoneId,
  type: 'AAAA',
  name: subdomain,
  content: '2001:db8::1',
  ttl: 3600,
  proxied: false,
});

NS Record (For Delegating a Subdomain)

await cloudflare.dns.records.create({
  zone_id: zoneId,
  type: 'NS',
  name: subdomain,
  content: 'ns1.otherdns.com',
  ttl: 86400,
});

Conclusion

This guide provides a structured way to manage Cloudflare DNS records through Firebase Functions, ensuring seamless subdomain management for your application. Additionally, you now know how to create, retrieve, list, and delete different types of DNS records to accommodate various use cases.

0
Enjoyed this article?
Subscribe to my newsletter for more insights and tutorials.