API Authentication

Hi All,

Please excuse me in advance, I am self-taught. My goal:

  • Use Cloudflare API with Python to automate email account creation.
  • For example, we give a list of names in the script (up to 1000), and the script creates all of these emails in Cloudflare.

Here is my code:

import requests
import json

# Cloudflare API endpoint for creating email forwarding rules
endpoint = 'https://api.cloudflare.com/client/v4/zones/{zone_id}/email/forwarders'

# Cloudflare API credentials
api_key = '<my API key>
email = '<my cloudflare email>'
zone_id = '<my zone ID>'

# Domain name and central inbox to forward emails to
domain_name = '<my domain name>'
central_inbox = '<my email address>'

# List of email accounts to create
email_accounts = ['user1', 'user2', 'user3']

# Loop through email accounts and create forwarding rules
for account in email_accounts:
    # Construct the payload for the API request
    payload = {
        'email': account + '@' + domain_name,
        'destination': central_inbox
    # Set headers for the API request
    headers = {
        'X-Auth-Email': email,
        'X-Auth-Key': api_key,
        'Content-Type': 'application/json'
    # Make the API request
    response = requests.post(endpoint.format(zone_id=zone_id), data=json.dumps(payload), headers=headers)
    # Check if the request was successful
    if response.status_code == 200:
        print('Successfully created email forwarding rule for {}'.format(payload['email']))
        print('Error creating email forwarding rule for {}: {}'.format(payload['email'], response.content))

The Error

What I have checked/done

  1. I have copied and pasted the ZONE ID and confirmed it’s correct.
  2. I have tried the global API.
  3. I have set up my own API with zone and email read/write permissions.
  4. I have tried with a different endpoint:
endpoint = 'https://api.cloudflare.com/client/v4/accounts/35e0c1a969f0972c4478901a299a4701/email/routing/addresses'

Hoping for some help to resolve this issue. Sorry in advance if this has been resolved, I couldn’t find the solution.