Create DNS record (PHP)

Hi, I’ve create this piece of code to create a new DNS Record in my cloudflare domain

function createDNSRecord($zoneID, $apiKey, $recordName, $recordType, $recordContent, $recordTTL = 3600) {
    $apiEndpoint = "$zoneID/dns_records";

    $data = array(
        "content" => $recordContent,
        "name" => $recordName,
        "type" => $recordType,
        "ttl" => $recordTTL

    $headers = array(
        "Content-Type: application/json",
        "Authorization: Bearer $apiKey"

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiEndpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);
    $error = curl_error($ch);


    if ($error) {
        return "cURL Error: $error";
    } else {
        $responseData = json_decode($response, true);
        if (isset($responseData['success']) && $responseData['success']) {
            return "DNS record created successfully.";
        } else {
            return "Failed to create DNS record. Error: " . print_r($responseData['errors'], true);

I’ve putted my email, api key (created here and my zone id but it return me the error 10000 (Authentication error)


Where are you using your email? I don’t see an X-Auth-Email header in your code.

Are you using an API Key or an API Token?

1 Like

API Token with Edit and read permission in the domain

1 Like

Have you looked at Troubleshooting · Cloudflare Fundamentals docs yet?

I use

createDNSRecord($zoneID, $apiKey, $recordName, $recordType, $recordContent, 3600);

to create a DNS record and it return me this error:

Failed to create DNS record. Error: Array
    [0] => Array
            [code] => 9106
            [message] => Missing X-Auth-Key, X-Auth-Email or Authorization headers


Yeah, the token is valid

{"result":{"id":"XXXXX","status":"active"},"success":true,"errors":[],"messages":[{"code":10000,"message":"This API Token is valid and active","type":null}]}

Token permissions: - DNS:Read, DNS:Edit

With PHP
createDNSRecord("myzoneid", "myapikey", "", "A", "", 3600);

Is there a DNS:Write permission that you can grant?

Template Name Permission Resource
Edit Zone DNS DNS Write Zone

What do you mean? I am the owner of that cloudflare domain/account

In tokens No, there’s only DNS Read and Edit.

Maybe the documentation is out of date. I am not logged into an account with a DNS API token at the moment and I tend to consume those tokens in other people’s code. You may need to wait for another Community member to offer further insight.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.