Cloudflare API returns error 400 Bad Request

Hey everyone,

I am using the cloudflare API in a nodeJS app I am writing for our company.

I don’t know what I am doing wrong, but when I try to post to cloudflare’s API to create a domain and DNS entries, however, I keep on getting an error message that reads:

Request failed with status code 400

I am using axios to make the request, e.g.:

 const headers = {
    'X-Auth-Email': MY_EMAIL,
    'X-Auth-Key': MY_KEY,
    'Content-Type': 'application/json'
  }

  const account = MY_ACCOUNT_ID

  await axios
    .post(
      'https://api.cloudflare.com/client/v4/zones',
      { name: 'domainexample12345.com' , account: { id: account }, jump_start: true },
      { headers: headers }
    )
    .then((res) => {
      return res.result
    })
    .catch((err) => {
      console.log('did not create DNS entries: ', JSON.stringify(err))
      return 1
    })

When I use cURL, it works fine. I have also tried using the cloudflare npm module and it just returns the same error.

I have also even tried using a token with full access and still get the same error.

Any help would be appreciated. Thanks !

Hi @software9 were you able to resolve this issue? If so, let us know how.
If similar request works in cURL but didn’t work in the code, the code may need some modifications. We can check the specific request you made that returned HTTP 400 in our logs if you open a support ticket.

Hi,

So it seems like the problem was caused in this specific case, was caused because the domain we tried to use for the DNS zone did not exist yet.

So if the domain does not exist for the zone you are creating, the api returns bad request, which made it difficult to troubleshoot.

It would be useful in such a case if the api can return something like “bad request - domain not found”