CloudflareAsyncAPI is an asynchronous Python client for interacting with Cloudflare's REST API v4 using either a Bearer token or a Global API Key.
This client supports creating zones, registering domains, adding DNS records, and checking zone status. https://dash.cloudflare.com/
- Authenticate using API token or global key
- Create zones and register domains
- Add DNS records (A, AAAA, CNAME, etc.)
- Wait for zone activation
- Structured exception handling
- Full support for asynchronous usage
You can authenticate in two ways:
cf = CloudflareAsyncAPI.from_api_token("your-api-token")cf = CloudflareAsyncAPI.from_global_key("[email protected]", "your-global-api-key")Here is a complete example of how to use the client or check example.py:
import asyncio
from cloudflare import CloudflareAsyncAPI # Adjust the import path as needed
async def main():
cf = CloudflareAsyncAPI.from_global_key("[email protected]", "your-global-api-key")
async with cf:
# Register a new domain
zone_id, ns1, ns2 = await cf.register_domain("example.com", fail_if_exists=False)
# Print zone info
print("Zone ID:", zone_id)
print("NS1:", ns1)
print("NS2:", ns2)
# Add an A record
await cf.add_dns_record(
zone_id=zone_id,
record_type="A",
name="www",
content="192.0.2.1",
proxied=True # Enable Cloudflare proxy
)
# Optionally wait until zone is active
await cf.wait_until_active(zone_id)
asyncio.run(main())The client raises custom exceptions for known Cloudflare error codes:
ZoneAlreadyExistsInvalidRequestHeadersIdenticalRecordExistsDNSRecordInvalidUserCredsInvalidExceededZonesLimit
This project is provided as-is under the MIT License.