back to home

cloudflare / cloudflare-python

The official Python library for the Cloudflare API

416 stars
110 forks
23 issues
PythonShellDockerfile

AI Architecture Analysis

This repository is indexed by RepoMind. By analyzing cloudflare/cloudflare-python in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.

Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.

Source files are only loaded when you start an analysis to optimize performance.

Embed this Badge

Showcase RepoMind's analysis directly in your repository's README.

[![Analyzed by RepoMind](https://img.shields.io/badge/Analyzed%20by-RepoMind-4F46E5?style=for-the-badge)](https://repomind.in/repo/cloudflare/cloudflare-python)
Preview:Analyzed by RepoMind

Repository Overview (README excerpt)

Crawler view

Cloudflare Python API library ) The Cloudflare Python library provides convenient access to the Cloudflare REST API from any Python 3.9+ application. The library includes type definitions for all request params and response fields, and offers both synchronous and asynchronous clients powered by httpx. It is generated with Stainless. MCP Server Use the Cloudflare MCP Server to enable AI assistants to interact with this API, allowing them to explore endpoints, make test requests, and use documentation to help integrate this SDK into your application. > Note: You may need to set environment variables in your MCP client. Documentation The REST API documentation can be found on developers.cloudflare.com. The full API of this library can be found in api.md. Installation Usage The full API of this library can be found in api.md. While you can provide a keyword argument, we recommend using python-dotenv to add to your file so that your API Email is not stored in source control. Async usage Simply import instead of and use with each API call: Functionality between the synchronous and asynchronous clients is otherwise identical. With aiohttp By default, the async client uses for HTTP requests. However, for improved concurrency performance you may also use as the HTTP backend. You can enable this by installing : Then you can enable it by instantiating the client with : Using types Nested request parameters are TypedDicts. Responses are Pydantic models which also provide helper methods for things like: • Serializing back into JSON, • Converting to a dictionary, Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set to . Pagination List methods in the Cloudflare API are paginated. This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually: Or, asynchronously: Alternatively, you can use the , , or methods for more granular control working with pages: Or just work directly with the returned data: Nested params Nested parameters are dictionaries, typed using , for example: File uploads Request parameters that correspond to file uploads can be passed as , or a instance or a tuple of . The async client uses the exact same interface. If you pass a instance, the file contents will be read asynchronously automatically. Handling errors When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of is raised. When the API returns a non-success status code (that is, 4xx or 5xx response), a subclass of is raised, containing and properties. All errors inherit from . Error codes are as follows: | Status Code | Error Type | | ----------- | -------------------------- | | 400 | | | 401 | | | 403 | | | 404 | | | 422 | | | 429 | | | >=500 | | | N/A | | Retries Certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors are all retried by default. You can use the option to configure or disable retry settings: Timeouts By default requests time out after 1 minute. You can configure this with a option, which accepts a float or an object: On timeout, an is thrown. Note that requests that time out are retried twice by default. Advanced Logging We use the standard library module. You can enable logging by setting the environment variable to . Or to for more verbose logging. How to tell whether means or missing In an API response, a field may be explicitly , or missing entirely; in either case, its value is in this library. You can differentiate the two cases with : Accessing raw response data (e.g. headers) The "raw" Response object can be accessed by prefixing to any HTTP method call, e.g., These methods return an object. The async client returns an with the same structure, the only difference being able methods for reading the response content. The above interface eagerly reads the full response body when you make the request, which may not always be what you want. To stream the response body, use instead, which requires a context manager and only reads the response body once you call , , , , , or . In the async client, these are async methods. The context manager is required so that the response will reliably be closed. Making custom/undocumented requests This library is typed for convenient access to the documented API. If you need to access undocumented endpoints, params, or response properties, the library can still be used. Undocumented endpoints To make requests to undocumented endpoints, you can make requests using , , and other http verbs. Options on the client will be respected (such as retries) when making this request. Undocumented request params If you want to explicitly send an extra param, you can do so with the , , and request options. Undocumented response properties To access undocumented response properties, you can access the extra fields like . You can also get all the extra fields on the Pydantic model as a dict with . Configuring the HTTP client You can directly override the httpx client to customize it for your use case, including: • Support for proxies • Custom transports • Additional advanced functionality You can also customize the client on a per-request basis by using : Managing HTTP resources By default the library closes underlying HTTP connections whenever the client is garbage collected. You can manually close the client using the method if desired, or with a context manager that closes when exiting. Semantic versioning This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions: • Changes that only affect static types, wit…