Python client for the Cloudflare v4 API
Project description
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
- Go to Dashboard
- Create Token
- Use
Edit Cloudflare Workers
template - Select one account from Account Resources
- Select
All Zones
or specific zones under Zone Resources - Continue to summary
- Create Token
- Copy the token and save it somewhere secret & secure.
Then run the following command in terminal:
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 poetry
poetry new cloudflare-app
cd cloudflare-app
poetry add cloudflare-api
Copy the code below in to a new file ./cloudflare-app/__main__.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 cloudflare-app
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.ini
file in your current working directory for future reference of the api token.
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 accountAccountData
AccountSettings
User
details
- Display details of the account's user
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 workerSubdomain
create
- Create a new subdomain if none existsget
- Get the current subdomain from cloudflare account
Cron
update
- Update an existing cron or create new cron for a workerget
- 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 namespaceNamespace
-
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 NamespaceMetadata
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:
- Update version in
__version__.py
inside package - Create a commit for the above change
- Tag the commit with VERSION
- Push the content in to main branch with tags
- Python publish workflow action activates
- Release the version pushed
- Build python package
- Publish python package in to pypi.org
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cloudflare_api-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93c019a6cd65fe1122f22d27934225246ef06c42b33cb48fe86e56ca2e490bc9 |
|
MD5 | 265ac02463b83ff64fc237557a299a91 |
|
BLAKE2b-256 | 88b1fd579f5f56479f455913c95756b819cef8375cdf404a6e8463c6dde7b400 |