---
title: "How To Programmatically Add DNS Records To Cloudflare In Node"
slug: "how-to-programmatically-add-dns-records-to-cloudflare-in-node"
published: "2025-03-21"
updated: "2025-12-21"
validated: "2025-10-20"
categories:
  - "Cloudflare"
llm-intent: "reference"
framework-versions:
  - "unspecified"
status: "stable"
llm-purpose: "Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records."
llm-prereqs:
  - "General familiarity with the article topic"
llm-outputs:
  - "Completed outcome: Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records."
---

**Summary Triples**
- (Install SDK, command, npm install cloudflare --save)
- (Store credentials, method, Use functions.params.defineSecret to define CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL, CLOUDFLARE_ZONE_ID)
- (Define config, example, Define ROOT_DOMAIN and SITE_BASE_URL with functions.params.defineString in Firebase Functions)
- (Create DNS record, operation, Use Cloudflare SDK client with zone ID to call zones.dnsRecords.add/create (create DNS A/CNAME/TXT as required))
- (List DNS records, operation, Use Cloudflare SDK client to list DNS records for a zone (filter by name/type if needed))
- (Retrieve DNS record, operation, Use DNS record ID with Cloudflare SDK to get details of a specific record)
- (Delete DNS record, operation, Use DNS record ID with Cloudflare SDK to delete a DNS record from the zone)
- (Use case, benefit, Dynamically manage subdomains and automate DNS lifecycle inside Firebase Functions)
- (Credentials source, link, Cloudflare API Keys available at https://dash.cloudflare.com/profile/api-tokens)
- (Security, recommendation, Store API keys as Firebase Function secrets, not in source code)

### {GOAL}
Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records.

### {PREREQS}
- General familiarity with the article topic

### {STEPS}
1. Follow the detailed walkthrough in the article content below.

<!-- llm:goal="Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records." -->
<!-- llm:prereq="General familiarity with the article topic" -->
<!-- llm:output="Completed outcome: Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records." -->

# How To Programmatically Add DNS Records To Cloudflare In Node
> Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records.
Matija Žiberna · 2025-03-21

## 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](https://dash.cloudflare.com/profile/api-tokens).
Choose **Global API Key**.
![cloudflare_account_api](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*jpYUlKiNUeRTGqnPoIQ96g.png)

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

## LLM Response Snippet
```json
{
  "goal": "Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records.",
  "responses": [
    {
      "question": "What does the article \"How To Programmatically Add DNS Records To Cloudflare In Node\" cover?",
      "answer": "Manage Cloudflare DNS programmatically in Node using Firebase Functions. Create, list, retrieve, and delete DNS records."
    }
  ]
}
```