Error Handling

Python SDK Error Handling

This page explains how the Demandbase Python SDK reports API errors, retries supported failures, and exposes error details to your application.

Error Types

The main SDK error type is demandbase.DemandBaseAPIError.

The SDK raises DemandBaseAPIError when:

  • The Demandbase API returns an HTTP error response.
  • The SDK prevents a request because required request values are missing.
  • A response has an unexpected shape for the requested SDK method.

Network-level errors, such as DNS failures, connection failures, and request timeouts, can be raised by the underlying HTTP client. Catch those separately if your application needs custom network retry or alerting behavior.

Basic Handling Pattern

import demandbase

try:
    with demandbase.DBClient(timeout=60.0, retry_count=2) as client:
        response = client.b2b_api.list_subscriptions(page=1, per_page=10)
        print(response.model_dump())
except demandbase.DemandBaseAPIError as error:
    print(f"Demandbase API error: {error.http_status_code} - {error.error_message}")
except Exception:
    # Handle network errors, timeouts, or unexpected application errors here.
    raise

DemandBaseAPIError Attributes

AttributeTypeDescription
request_pathstrAPI path associated with the failed request.
http_status_codeintHTTP status code returned by the API or generated by the SDK.
error_messagestrError message returned by the API or generated by the SDK.
request_headersmapping or NoneRequest headers, when available.
response_headersmapping or NoneResponse headers, when available. Useful for rate-limit handling.
payloadobject or NoneRequest payload, when available.
parametersobject or NoneQuery parameters, when available.

Example:

except demandbase.DemandBaseAPIError as error:
    print(error.request_path)
    print(error.http_status_code)
    print(error.error_message)
    print(error.response_headers)

Retries

DBClient accepts a retry_count argument.

import demandbase

with demandbase.DBClient(retry_count=2) as client:
    response = client.b2b_api.list_subscriptions()

The SDK retries these cases:

CaseSDK behavior
401 invalid or expired access tokenClears the cached token, requests a new token, and retries the request.
429 rate-limit responseRetries up to retry_count.
500, 502, 503, 504 server errorsRetries up to retry_count.

For 429 and supported 5xx responses, retry backoff starts at 30 seconds and doubles up to a maximum of 600 seconds.

If retries are exhausted, the SDK raises DemandBaseAPIError.

Rate Limits

If a rate-limit response is returned after configured retries are exhausted, catch DemandBaseAPIError and inspect:

  • error.http_status_code
  • error.response_headers
  • error.error_message

Example:

except demandbase.DemandBaseAPIError as error:
    if error.http_status_code == 429:
        print("Rate limit reached. Retry later.")
        print(error.response_headers)
    else:
        raise

Rate-limit header names can vary by API response. Use the returned headers to decide whether your application should retry later, queue the request, or reduce request volume.

Authentication Errors

The SDK reads credentials from environment variables:

  • DEMANDBASE_CLIENT_ID
  • DEMANDBASE_CLIENT_SECRET

If either value is missing, client initialization raises RuntimeError.

If credentials are present but invalid, or the credentials do not have access to the requested API, the SDK raises DemandBaseAPIError.

Checklist:

  • Confirm both environment variables are set in the runtime environment.
  • Confirm the client ID and client secret are valid.
  • Confirm the API Key Set includes the API permissions required by your integration.
  • Confirm your integration is calling the production Demandbase API.

Validation Errors

The SDK validates some inputs before sending requests.

ScenarioException
Page number is less than 1ValueError
Results-per-page value is less than 1ValueError
Local CSV file path cannot be readValueError
CSV input is not a pandas.DataFrame, bytes, or path stringValueError or TypeError, depending on the method
Required export job fields are missingDemandBaseAPIError

Example:

try:
    with demandbase.DBClient() as client:
        client.b2b_api.list_subscriptions(page=0)
except ValueError as error:
    print(f"Invalid request value: {error}")

Logging And Sensitive Data

SDK log output redacts bearer access tokens from authorization headers.

Request payloads, query parameters, and response bodies can still contain sensitive business data. Avoid logging full request or error context in production unless your application has appropriate access controls and retention policies.

Enable SDK logs when troubleshooting:

import demandbase

demandbase.enable_logging("INFO")

Use DEBUG only during focused troubleshooting because it can produce verbose request diagnostics.

Troubleshooting Guide

SymptomWhat to check
Missing credential errorConfirm DEMANDBASE_CLIENT_ID and DEMANDBASE_CLIENT_SECRET are set before creating DBClient.
401 persists after retryConfirm the credentials are valid and have not been rotated or revoked.
403 or permission-related errorsConfirm the API Key Set has permission for the endpoint or data type being requested.
429 responsesReduce request volume, increase spacing between requests, or retry later based on response headers.
Timeout errorsConfirm outbound network access and increase timeout, in seconds, for requests that normally take longer.
Unexpected response dataConfirm request filters, entity type, and account permissions match the data you expect to receive.

Related Pages