BuildWithMatija
  1. Home
  2. Blog
  3. Cloudflare
  4. How To Programmatically Add DNS Records To Cloudflare In Node

How To Programmatically Add DNS Records To Cloudflare In Node

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

21st March 2025··
Cloudflare
How To Programmatically Add DNS Records To Cloudflare In Node

☁️ Cloudflare Edge Development Guides

Complete Cloudflare guides with practical examples, deployment strategies, and developer prompts to help you build and ship edge applications faster.

No spam. Unsubscribe anytime.

📄View markdown version
0

About the author

Matija Žiberna

Matija Žiberna

Full-stack developer, co-founder

AboutResume

Self-taught full-stack developer sharing lessons from building software and startups.

I'm Matija Žiberna, a self-taught full-stack developer and co-founder passionate about building products, writing clean code, and figuring out how to turn ideas into businesses. I write about web development with Next.js, lessons from entrepreneurship, and the journey of learning by doing. My goal is to provide value through code—whether it's through tools, content, or real-world software.

Contents

  • Introduction
  • Prerequisites
  • Step 1: Install Cloudflare SDK
  • Step 2: Configure Environment Variables
  • Step 3: Cloudflare DNS Management Functions
  • Creating a DNS Record
  • Listing All DNS Records
  • Retrieving a Specific DNS Record
  • Deleting a DNS Record
  • Adding Different DNS Record Types
  • Conclusion
On this page:
  • Introduction
  • Prerequisites
  • Step 1: Install Cloudflare SDK
  • Step 2: Configure Environment Variables
  • Step 3: Cloudflare DNS Management Functions
Build with Matija logo

Build with Matija

Modern websites, content systems, and AI workflows built for long-term growth.

Services

  • Headless CMS Websites
  • Next.js & Headless CMS Advisory
  • AI Systems & Automation
  • Website & Content Audit

Resources

  • Case Studies
  • How I Work
  • Blog
  • Topics
  • CMS Hub
  • E-commerce Hub
  • B2B Website Strategy
  • Dashboard

Headless CMS

  • Payload CMS Developer
  • CMS Migration
  • Multi-Tenant CMS
  • Payload vs Sanity
  • Payload vs WordPress
  • Payload vs Contentful

Get in Touch

Ready to modernize your stack? Let's talk about what you're building.

Book a discovery callContact me →
© 2026Build with Matija•All rights reserved•Privacy Policy•Terms of Service
BuildWithMatija
Get In Touch

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_apicloudflare_account_api

Step 1: Install Cloudflare SDK

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

bash
npm install cloudflare --save

Step 2: Configure Environment Variables

Store your Cloudflare credentials securely using Firebase environment variables:

javascript
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:

avascript
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

javascript
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:

javascript
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:

javascript
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)

avascript
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.)

javascript
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)

javascript
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)

javascript
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)

javascript
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.