Retrieve account ID to create first zone

To create a zone through the API you need an account ID. But to get an account ID you need to create a zone. Feels like the chicken or the egg dilemma.

How to get my account ID to create a zone if there are no existing zones yet? Preferably, I want to get the account ID through an API call instead of looking it up somewhere on the dashboard.

Bonus question: Can you have multiple account ID’s? If so, how?

The global API key should be under your profile settings…
https://dash.cloudflare.com/profile/api-tokens

The API key is used to authenticate with the API so you can’t get your API key from the API.

As API global keys give full access, API use is best done programmatically by creating tokens with only the permissions needed.

I don’t use the global API and I am not looking for this key. I already have an API token and now I am looking for the account ID that is required for Zones Post command.

Ask the API for the account list…

I have seen that command but it does not list any account. It also does not include an ID according to the documentation so it does not seem to be the account ID that is usually listed in the zone information.

Works for me…

url --request GET \
  --url https://api.cloudflare.com/client/v4/accounts \
  --header 'Content-Type: application/json' \
  --header 'X-Auth-Email: xxxxxxxxxx \
  --header 'X-Auth-Key: xxxxxxxxx'
{"result":[{"id":"1d975dfb4e5xxxxxxxxxxxxx","name":"xxxxxxx","type":"standard","settings":{"enforce_twofactor":false,"api_access_enabled":null,"access_approval_expiry":null,"use_account_custom_ns_by_default":false,"default_nameservers":"cloudflare.standard"},"legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}},"created_on":"2022-03-27T02:46:24.910070Z"}],"result_info":{"page":1,"per_page":20,"total_pages":1,"count":1,"total_count":1},"success":true,"errors":[],"messages":[]}%
1 Like

Or you can get just it from the dashboard URL in the browser…

Thank you! I now see that it was a permission issue with the API token. You need to have Account Settings enabled to get a good result for that call.

Too bad Cloudflare does not show a permission warning with this call to notice that you do not have the right permissions.

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