Skip to main content

Python client for the Cloudflare v4 API

Project description

Publish Package PyPI - Status GitHub release (latest by date) PyPI CodeFactor GitHub license

Python client for Cloudflare API v4

Require Python 3.9+

Usage

Python Package

pip install cloudflare-api

Sample code can be found inside /test.py

Source Code

git clone https://github.com/nikhiljohn10/cloudflare-api
cd cloudflare-api

Instructions to get new API Token

  1. Go to Dashboard
  2. Create Token
  3. Use Edit Cloudflare Workers template
  4. Select one account from Account Resources
  5. Select All Zones or specific zones under Zone Resources
  6. Continue to summary
  7. Create Token
  8. Copy the token and save it somewhere secret & secure.

Then run the following command in terminal:

make init
. venv/bin/activate
make test

If it is running for first time, you will be asked to enter API Token. Paste the api token copied from Dashboard.

Example

For this example, poetry is used for easy setup.

python3 -m pip install --user poetry
mkdir -p myapp && cd myapp
poetry init -n
poetry add cloudflare-api

Copy the code below in to a new file ./myapp/app.py.

#!/usr/bin/env python3

from CloudflareAPI import Cloudflare

def main():
  cf = Cloudflare()

  print(cf.account.list())
  print(cf.user.details())
  print(cf.worker.list())
  print(cf.store.list())

if __name__ == "__main__":
    main()

You can now run the program using following command:

poetry run python app.py

If it is running for first time, you will be asked to enter API Token. Paste the api token copied from Dashboard. This will create cf-config.ini file in your current working directory for future reference of the api token.

You can find more examples in examples directory

Default Permissions

1. Account

  • Workers Tail ( Read )
  • Workers KV Storage ( Edit )
  • Workers Scripts ( Edit )
  • Account Settings ( Read )

2. Zones

  • Workers Routes ( Edit )

3. Users

  • User Details ( Read )

Available endpoints

Account

  • list - List all accounts where given token have access

  • get_id - Return account id if only one account exists. Otherwise display all accounts availabe and exit.

  • details - Display details of an account

  • rename* - Rename an existing account

    AccountData

    AccountSettings

User

  • details - Display details of the account's user

    UserData

Worker

  • list - List all existing workers

  • upload - Upload a new worker with binding if given

    • Upload file along as javascript
    • Upload file along with metadata as multipart form-data
      • KV Bindings
      • Environment variables
      • Secrets
  • download - Download an existing worker

  • deploy - Deploy an existing worker using the subdomain

  • undeploy - Undeploy an existing worker

  • delete - Delete an existing worker

    Subdomain

    • create - Create a new subdomain if none exists
    • get - Get the current subdomain from cloudflare account

    Cron

    • update - Update an existing cron or create new cron for a worker
    • get - Get the cron task list of specified worker

Store(Workers KV)

  • list - List all existing Namespaces

  • get_ns - Returns requested Namespace object

  • create - Create a new namespace

  • rename - Rename an existing namespace

  • delete - Delete an existing namespace

    Namespace

    • keys - Display all the keys in the Namespace

    • read - Read the value of given key in the Namespace

    • write - Write given key-value pair to the Namespace

    • bulk_write - Write given key-value pairs in bulk to the Namespace

    • delete - Delete given key from the Namespace

    • bulk_delete - Delete given keys in bulk from the Namespace

      Metadata

      NSKey

      NSBundler

      • add - Add key-value pair with metadata to bundler

( * : Not accessable with default Worker permissions )

Development

Version Bump

To display current version:

make version

To bump to new version, where x.y.z is major,minor & patch versions respectively:

make VERSION=x.y.z bump

The above command does the following:

  1. Update version in __version__.py inside package
  2. Create a commit for the above change
  3. Tag the commit with VERSION
  4. Push the content in to main branch with tags
  5. Python publish workflow action activates
  6. Release the version pushed
  7. Build python package
  8. Publish python package in to pypi.org

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudflare-api-2.1.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

cloudflare_api-2.1.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file cloudflare-api-2.1.0.tar.gz.

File metadata

  • Download URL: cloudflare-api-2.1.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for cloudflare-api-2.1.0.tar.gz
Algorithm Hash digest
SHA256 d700740bff16b418e97eb15edda0d00b12218e3931c827b02fbca8a3bafca470
MD5 45b796b8fd141c15fa49b8b7de29b9cf
BLAKE2b-256 0c5f43d5ae97e950f855286098ab4a0240d8e180cbc7f4f3c83fc5d5eaa3e489

See more details on using hashes here.

File details

Details for the file cloudflare_api-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: cloudflare_api-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for cloudflare_api-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39b7c3809ba038b094268a7e643681f0f4ca129cbb3b2f3a1fcda9a1dd6f6948
MD5 0192ba8365a5ef7053290f88fcc82dfa
BLAKE2b-256 0eb68c2a83f90cea78655f55275b03a3cf7f49c621d9736669d52c15e809f658

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page