Change DNS Record with Cloudflare API

Hello!

I’m trying to update a record via the API. It would essentially change back and forth between two presets.
The first thing I’m doing is checking the current record, whether it’s an A or CNAME record, and changing it to the opposite.

Fetching the record works fine, there’s no issue there. But once I get to updating it’s where it goes wrong. It’s done with JavaScript → Axios, this is my code:

async function updateDnsRecord(client, bergenStatus) {
  const oldRecord = await fetchDNSRecordAPI();

  if (!oldRecord || !oldRecord.result) {
    console.log(`Error fetching DNS Record: ${oldRecord}`);
    return;
  }

  const { id, type } = oldRecord.result;

  if (bergenStatus === 200) {
    if (type !== "A") {
      const newType = "A";
      const newContent = process.env.BERGEN_STATUS_IP;
      const updatedRecord = {
        content: newContent,
        name: "status",
        proxied: true,
        type: newType,
        comment: "status subdomain - main",
        tags: ["owner:dns-team"],
        ttl: "3600",
      };
      const response = await updateDnsRecordAPI(id, updatedRecord);
      console.log(response);
    } else {
      console.log(`Bergen Status 200; Record Type is already A, returning...`);
    }
  } else {
    if (type !== "CNAME") {
      const newType = "CNAME";
      const newContent = process.env.BERGEN_STATUS_URL_RESERVE;
      const updatedRecord = {
        content: newContent,
        name: "status",
        proxied: true,
        type: newType,
        comment: "status subdomain - reserve",
        tags: ["owner:dns-team"],
        ttl: "3600",
      };
      const response = await updateDnsRecordAPI(id, updatedRecord);
      console.log(response);
    } else {
      console.log(
        `Bergen Status NOT 200; Record Type is already CNAME, returning...`
      );
    }
  }
}

async function updateDnsRecordAPI(recordId, updatedRecord) {
  const zoneId = process.env.CF_ZONE_ID;
  const apiEmail = process.env.CF_API_KEY_EMAIL;
  const apiKey = process.env.CF_API_KEY;

  const options = {
    method: "PUT",
    url: `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${recordId}`,
    headers: {
      "Content-Type": "application/json",
      "X-Auth-Email": apiEmail,
      "X-Auth-Key": apiKey,
    },
    data: { updatedRecord },
  };

  axios
    .request(options)
    .then((response) => {
      console.log(response.data);
    })
    .catch((err) => {
      console.error(err);
    });
}

I’ve tried different API Keys, such as one I created under “API Tokens → Gave it Zone.DNS perms”. Also the “Global API Key”, none of them worked.
I’m not sure what the error I get means, it’s returning “status: 400, statusText: bad request”, there’s “ERR_BAD_REQUEST” as well.

So I can’t seem to figure out what the error even is, that’s why I’m creating the post. I saw another post pretty similar, but there the issue seemed with the X-Auth-Email and Key, or the ttl being to little. This might be a similar issue, but not what I can tell.