Python API Client Script Prompt
Build a robust Python client script for a REST API with session reuse, auth handling, pagination, rate-limit awareness, and typed responses — suitable for automation jobs rather than throwaway one-liners.
- Target user
- Automation engineers integrating scripts with REST APIs
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a backend automation engineer who has written API clients that quietly run in cron for years without paging anyone. I will provide: - The API (base URL, auth scheme, key endpoints) or its OpenAPI/docs - What the script must do (read, sync, bulk create) - Rate limits, pagination style, and any idempotency support Build a production-quality client script: 1. **Session + connection reuse** — a `requests.Session` (or `httpx.Client`) with a base URL, default headers, and an `HTTPAdapter` mounting connection pooling; never create a new connection per call in a loop. 2. **Auth** — load the token/key from an env var or secret file, never hard-code it; support token refresh if OAuth; mask secrets in any debug logging. 3. **Timeouts everywhere** — every request gets an explicit `(connect, read)` timeout; a request with no timeout is a hung cron job waiting to happen. 4. **Status handling** — raise on 4xx/5xx with the response body included in the error; classify retryable (429/5xx) vs fatal; honor `Retry-After`. 5. **Pagination** — implement the API's scheme (cursor, `Link` header, page/offset) as a generator that yields items lazily so memory stays flat for large result sets. 6. **Rate limits** — read remaining-quota headers and proactively slow down; back off on 429 rather than hammering. 7. **Structure** — a small client class with methods per resource, type hints, and dataclasses/pydantic models for responses so callers get real objects, not raw dicts. 8. **CLI entry** — a thin `argparse`/Click wrapper so the same module runs as a script and imports as a library; `--output json` for piping. 9. **Testing** — tests using `responses`/`respx` to mock the API, covering pagination, a 429-then-success retry, and an auth failure. Output: (a) the client class, (b) the CLI wrapper, (c) the model definitions, (d) mocked tests, (e) a `.env.example` listing required secrets. Bias toward: explicit timeouts, lazy pagination, secrets from env, real types over dicts.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Word-Splitting and Quoting Hardening Prompt
Audit and rewrite a Bash script to eliminate unquoted-expansion bugs, unsafe word splitting, and glob injection while preserving intended behavior
-
Bash Exit-Code and Pipefail Propagation Audit Prompt
Audit a Bash script for swallowed failures, missing pipefail, and broken exit-code propagation through pipes, subshells, and command substitution
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
-
Bash Sourceable Function Library Pattern Prompt
Refactor scattered Bash helpers into a safe, namespaced, sourceable library that can be reused across scripts without polluting the caller's environment
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.