The problem
Our quote flow looks up vehicle details from a registration number through third-party registry vendors. There is more than one vendor, different partner channels use different ones, and vendors change their APIs, raise prices or go down.
The usual approach is to write each vendor as code: a client class, request building, response parsing, tests and a release. Then adding or switching a vendor waits for a deploy, and during a vendor outage that is far too slow.
The observation that changed the design: many API vendors already ship a Postman collection. The request shapes, headers, auth and example bodies are already written down, in a machine-readable format. So instead of translating the collection into code, we store it and run it.
The shape of a vendor record
A vendor is a database row holding JSON, editable from the internal admin:
- The Postman collection, exactly as exported, with
{{variables}}for anything dynamic. - A map per lookup type: which request in the collection to call, the variables per environment (staging and production credentials differ), and how to read the response.
- A field mapping from the vendor's response to our internal vehicle fields.
{
"custom_items": {
"registration_no_variable": "reg_no",
"registration_no_delimiter": "-" // MH-12-AB-1234
},
"map": {
"QUOTE": {
"title": "Get RC details",
"variables": { "UAT": {...}, "PROD": {...} },
"response_parser_helper": {
"response_drill": "result.data",
"response_success_attribute": "status",
"response_success_status": "OK",
"response_not_found_code": "NOT_FOUND"
}
}
}
}
Small vendor quirks live in that config too: whether the registration number is sent as MH12AB1234, MH-12-AB-1234 or MH 12 AB 1234, and which variable receives the end user's IP when a vendor asks for it.
The runner: a trimmed copy of postpy2
To execute collections I started from postpy2, a small open-source library that loads a Postman collection and exposes each request as a Python call. Then I took things out.
The library was written for developers running their own collections on their own machine. Here the collection is configuration that admins can edit in production, so every feature is attack surface. The copy we run:
- drops request overrides, so a caller cannot merge arbitrary
requestsarguments into a call, - drops file handling and form-data bodies, so a collection cannot read or upload files from the server,
- loads environments from JSON in the database, never from file paths,
- uses no libraries beyond what the project already depends on,
- adds the two things the vendors needed: basic auth, and auth defined per request instead of per collection.
One small guard earned its place: before sending, the runner checks the final URL and refuses to send if any {{variable}} is still unreplaced. A typo in a variable name fails loudly instead of calling a half-built URL.
The trimmed copy is vendored into the codebase rather than installed as a package. Because the changes remove features, that has a useful side effect: an upstream release can never quietly bring a removed feature back.
Turning responses into outcomes
Every vendor reports success and failure differently: HTTP status codes, a status string in the body, an error code buried two levels down. The map's response helper turns each response into one of a few outcomes:
| Outcome | Meaning | What happens next |
|---|---|---|
SUCCESS | Vehicle found and parsed | Mapped to internal fields and stored |
NOT_FOUND | Vendor answered: no such vehicle | Logged, so that vendor is not asked again soon; the next vendor in the chain is tried |
API_DOWN | No usable response | Next vendor in the chain |
UNKNOWN_RESPONSE | Response did not match the map | Next vendor in the chain; worth a look |
Every call is logged with the vendor, lookup type, HTTP status, response headers, raw response and outcome. A decorator also records where the call came from (the calling function and file) and, when there is a request, the end user's IP and user agent. When a vendor disputes a bill or a partner reports a wrong vehicle, the answer is one query away.
Not paying twice
Vendors charge per call, so the cheapest call is the one you do not make:
- a vehicle already stored is served from the database unless it is old enough to refresh,
- a recent
SUCCESSorNOT_FOUNDfor the same number, vendor and lookup type is not repeated within a configurable number of days, - an explicit refresh is available for the cases that need fresh data.
Caching NOT_FOUND matters as much as caching success: a mistyped or fake number would otherwise be paid for every time someone retries it.
Failover as an ordered list
Because a vendor is just a row, failover is just an ordered list of rows. Each lookup type has a priority list of vendors; a lookup walks the list and returns the first success:
def lookup_with_fallback(lookup_type, reg_no):
response = None
for vendor in vendor_chain(lookup_type): # admin-ordered
response = call_vendor(vendor, lookup_type, reg_no)
if response.success:
return response
return response # the last failure
Reordering vendors, or adding a new one at the end of the chain, is an admin change with no deploy. Each lookup type has its own chain, so each can prefer a different vendor.
The trade-offs
- Configuration is code now. A wrong drill path breaks a vendor as surely as a bad commit. Admin changes need the same care as a release: who can edit, and a record of who did.
- Debugging moves into data. "Why did this vendor fail?" is answered from the call log, not from a stack trace, so the log has to be complete.
- Tests need a mock. Outside production the runner can return canned responses per vendor, so the flow can be tested without paying for calls.
What I would add next
A runner that executes admin-editable configuration should enforce limits the configuration cannot override:
- a timeout on every outbound call, set by the runner, not the collection,
- an allow-list of vendor hosts, so an edited URL cannot point the server at an internal address,
- schema validation on save for the map and field mapping, so a broken config is rejected in the admin instead of failing at lookup time,
- a circuit breaker per vendor, so a vendor that is down is skipped for a while instead of being tried first on every lookup.
Takeaways
- If vendors already describe their API in a machine-readable format, consider running that description instead of translating it into code.
- When configuration becomes executable, treat every library feature as attack surface and remove what you do not need.
- Normalize every vendor's response into a small set of outcomes, and log all of them.
- Cache "not found" as well as "found" when every call costs money.
- Make failover an ordered list in data, so changing it during an outage takes a minute, not a release.