Developer guide
OFAC API in Python: screening and error handling
Build OFAC screening in Python with a complete HTTP example, explicit source selection, idempotency, safe failures, and review handoffs.
On this page
What this Python integration does
OFAC is the U.S. Treasury authority that publishes the SDN and non-SDN sanctions data. This tutorial calls SanctionsKit, not an OFAC-operated screening endpoint. Keep the original authority and list identity visible in each result.
The client submits a subject, validates the response, and prints only the screening ID, status, and candidate count. It does not approve a customer, resolve an identity, or make a legal decision. A nonzero exit leaves the local operation incomplete.
Prepare the runtime and a synthetic request
Use Python 3.11 or later. The HTTP client uses urllib, json, and the standard library, so the screening example needs no pip package or provider SDK.
Save the following as request.json. Mira Calder is an invented subject and sandbox@1 uses synthetic fixtures, not a current production sanctions search. Set SANCTIONSKIT_API_KEY to your test key in the process environment. Set SCREENING_OPERATION_ID to a new persistent identifier for this operation, and reuse it only for an unchanged retry.
{
"subject": {
"name": "Mira Calder",
"entityType": "person",
"birthDate": "1984"
},
"package": "sandbox@1",
"retention": "standard"
}Send the request and validate the result
Save the client as screen.py. The endpoint is fixed to SanctionsKit’s HTTPS API. The example rejects redirects, requires explicit authorization before using a live key, and does not retry automatically. Production integrations should preserve the operation state before the first network call.
"""Python 3.11+. Trusted backend/CLI. Uses only the standard library."""
import json
import os
import sys
from pathlib import Path
from urllib.error import HTTPError, URLError
from urllib.request import Request, build_opener, HTTPRedirectHandler
ENDPOINT = "https://www.sanctionskit.com/api/v1/screenings"
MAX_BYTES = 8 * 1024 * 1024
class NoRedirect(HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
def main() -> None:
key = os.environ.get("SANCTIONSKIT_API_KEY", "")
operation = os.environ.get("SCREENING_OPERATION_ID", "")
if not key or not operation:
raise ValueError("Set the API key and a persistent operation ID.")
if not key.startswith("sk_test_") and not (
key.startswith("sk_live_") and os.environ.get("ALLOW_LIVE_SCREENING") == "yes"
):
raise ValueError("Use a test key, or explicitly authorize a live screening.")
raw = Path(sys.argv[1] if len(sys.argv) > 1 else "../request.sandbox.json").read_bytes()
if len(raw) > 64 * 1024:
raise ValueError("Request file exceeds this example's 64 KiB limit.")
body = json.loads(raw)
if not isinstance(body, dict) or not body.get("subject") or ("sources" in body) == ("package" in body):
raise ValueError("Supply a subject and exactly one of sources or package.")
request = Request(ENDPOINT, data=json.dumps(body).encode(), method="POST", headers={
"Authorization": f"Bearer {key}", "Content-Type": "application/json", "Idempotency-Key": operation,
})
try:
with build_opener(NoRedirect()).open(request, timeout=25) as response:
status, payload = response.status, response.read(MAX_BYTES + 1)
except HTTPError as error:
with error:
status, payload = error.code, error.read(MAX_BYTES + 1)
if len(payload) > MAX_BYTES:
raise ValueError("Incomplete response: size limit exceeded.")
envelope = json.loads(payload)
if not 200 <= status < 300:
detail = envelope.get("error", {}) if isinstance(envelope, dict) else {}
code = detail.get("code", "unknown") if isinstance(detail, dict) else "unknown"
raise ValueError(f"Screening incomplete: HTTP {status}, code {code}.")
data = envelope.get("data") if isinstance(envelope, dict) else None
if not isinstance(data, dict) or not isinstance(data.get("id"), str) or not data["id"]:
raise ValueError("Incomplete response: missing screening ID.")
state, matches = data.get("status"), data.get("matches")
if state not in ("potential_match", "no_match") or not isinstance(matches, list):
raise ValueError("Incomplete response: unexpected screening shape.")
if (state == "no_match" and matches) or (state == "potential_match" and not matches):
raise ValueError("Incomplete response: inconsistent screening shape.")
print(json.dumps({"id": data["id"], "status": state, "candidateCount": len(matches)}))
if __name__ == "__main__":
try:
main()
except (OSError, ValueError, URLError) as error:
# Do not print the request, credential, or raw API response.
print(f"Screening did not complete: {type(error).__name__}. Check the operation before retrying.", file=sys.stderr)
sys.exit(1)Run the client
Run this from the directory containing request.json and the project files. Keep the API key out of committed files and shell tracing. The tutorial’s request-file limit is 64 KiB and its response parsing limit is 8 MiB; these are client safeguards, not claims about API service limits.
urllib raises HTTPError for unsuccessful HTTP responses, so handle that branch separately from network errors. Disable redirects and bound the body read. Validate object, string, and list types explicitly; an exception must not return an empty matches array.
python screen.py request.jsonChange to the required production coverage
The production example selects ofac-sdn only. Add ofac-non-sdn only when your required scope includes it, after checking current availability and compatibility. An SDN-only check is not a complete non-SDN check, and non-SDN records do not all have the same restrictions.
For an authorized production operation, replace the package field with the sources array shown below, supply the actual lawfully held subject details, use a live key, and set ALLOW_LIVE_SCREENING=yes. Keep the complete request in request.json and create a new operation ID. Do not send both sources and package. The fragment below is a coverage change, not a complete request.
{
"sources": [
"ofac-sdn"
]
}Preserve the source details a reviewer needs
Preserve source record IDs, list names, program context, aliases, date precision, and the actual fields that support or conflict with a candidate. A no-match result does not resolve OFAC’s 50 Percent Rule, geography-based restrictions, or the legality of a proposed activity.
Retain the screening ID with your internal subject reference. Retrieve the complete evidence through the documented retained-result workflow when authorized; the console summary deliberately does not print raw identity details. A later analyst decision must not overwrite the original screening outcome.
Handle failures and test the source-specific edge cases
Treat HTTP errors, timeouts, malformed JSON, missing fields, unknown statuses, and unavailable required coverage as incomplete checks. A 401 or 403 needs credential or scope correction. A 409 needs idempotency-conflict handling. A 429 needs its specific rate-limit or usage-cap treatment; it is not a no-match result.
Test similar names with different reliable birth years, overlapping year-only dates, and a source record with no comparable identifier. The absence of an identifier is unknown, not a conflict.
Use unittest or your existing test runner to replace the opener with synthetic responses. Exercise HTTPError, malformed JSON, a missing data object, and an unknown status as well as the two valid result paths.
- Verify both valid result states and inconsistent status/matches combinations.
- Retry an accepted operation only with its original key and unchanged payload.
- Keep missing identity facts unknown instead of inventing contradictory values.
- Route potential matches into review before the separate business decision.
- Keep secrets, submitted names, and raw error bodies out of routine logs.