Adding a site, how to get zone_id

I’m writing a bash script to add 35 sites, and at the same time make some changes and add rules. But all of those changes and rules need the sites’ zone_id.

I’m looking at the docs, though, and I don’t think that zone id is returned! And it looks like .id, .result.account.id, and .owner.id are all the same thing: the account id that I had manually submitted with the curl.

Am I wrong? I get confused with a lot of names (eg, saying “zone” instead of “domain” or “site”), so it’s easy for me to overlook it.

This is the script I’m using for this:

zone_id=$(curl https://api.cloudflare.com/client/v4/zones
-H ‘Content-Type: application/json’
-H ‘X-Auth-Email: $email’
-H ‘X-Auth-Key: $key’
–data ‘{
“account”: {
“id”:“$account”
},
“name”: "’“$domain”‘",
“type”: “full”
}’` | jq ‘.foo’)

This will return the list of zone IDs…

#!/bin/bash

CF_AUTH_EMAIL="[email protected]"
CF_AUTH_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxx"

ZONE_IDS=`
curl -s --request GET \
  --url https://api.cloudflare.com/client/v4/zones \
  --header "Content-Type: application/json" \
  --header "X-Auth-Email: ${CF_AUTH_EMAIL}" \
  --header "X-Auth-Key: ${CF_AUTH_KEY}" \
 | jq -r '.result[] | "\(.id)"'
`

Ahh. I’m doing this in a loop and was hesitant to add a ton of curl requests to the loop, but I guess I don’t have a choice :-/

I’m trying to use that to get JUST the ID of the domain that I want, but this is returning blank (I’m setting “domain” manually for the test):

domain=mysite.com
zone_id=$(curl …) | jq ‘first(.result | select(.name==“$domain”).id)’)

I also tried it with the “(…)” like you had in your example, but it also returned blank. And I tried it without $domain in quotes, but that returned an error.

I ran the curl without jq to get the full list, and I’m positive that the domain I’m entering is in the results.

Any suggestions?

Nevermind, I figured it out :slight_smile: I had to add name= to the query string, like so:

zone_id=$(curl --request GET
–url https://api.cloudflare.com/client/v4/zones/?name=$domain
…)

The docs say that you can use these filter operators, but I’m not sure how you would include those in a query string:

  • equal (default)
  • not_equal
  • starts_with
  • ends_with
  • contains
  • starts_with_case_sensitive
  • ends_with_case_sensitive
  • contains_case_sensitive

Yes, you usually have to loop round and do a command for each one. Just watch for the limit of 1200 requests per 5 minutes on the API.

You got the query sorted. Documentation suggests to add the filters as ?name=starts_with:$domain but I’ll leave you to try that.

1 Like

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