Is there no way to create an "R2" DNS record with the API?

I would like to fully automate the process of connecting an R2 bucket to a Cloudflare domain. Based on this thread, it seems that there is no way to do this without the dashboard. Is that really the case?

It’s not really the case no, an API exists for it (the exact same as the dashboard), it’s just undocumented and could change at any time without warning:
You can see in network tools, the dash does something like this:
POST /accounts/${account_id}/r2/buckets/${bucket}/custom_domains
domain: ${domain}
zoneId: ${zone_id}
zoneName: ${zone_name}
and then
PUT /accounts/{account_id}/r2/buckets/{bucket}/policy?cname=${domain}&access=PublicUrlAndCnames
to allow that cname access to your bucket.

There was talk from engineers working on R2 about changing it a bit and documenting it but that was a while ago and nothing since. Hopefully eventually.

I see in the post you linked, sdayman said

UPDATE: It looks like the Allow part is dashboard-only. As none of this is not documented in the API, this is probably as close as you’re going to get for now.

Either something changed or the non-documentation part of it was confusing, I set up a R2 Custom Domain entirely through the API just now to test that and it works perfectly fine. The allow part is the policy endpoint.

1 Like