Python SDK

The Python SDK provides typed, synchronous access to the Covertier API. It supports Python 3.11 and works in Flask applications and other Python services.

Installation

Install the SDK from PyPI with pip:

python -m pip install covertier

The SDK uses urllib3 for HTTP requests and does not add Flask as a dependency.

Initializing the Client

Store your access token in an environment variable and initialize a client for the sandbox environment:

import os

from covertier import Client, Environment

client = Client(
    access_token=os.environ["COVERTIER_ACCESS_TOKEN"],
    environment=Environment.SANDBOX,
)

Use the client as a context manager when possible so its connection pool is closed after use:

with Client(
    access_token=os.environ["COVERTIER_ACCESS_TOKEN"],
    environment=Environment.SANDBOX,
) as client:
    makes = client.get_vehicle_makes()

You can also call client.close() explicitly. For a self-hosted or local API, pass base_url instead of environment. Set timeout to a number for a total timeout, or to a (connect, read) tuple for separate connection and read timeouts.

Getting Vehicle Makes

makes = client.get_vehicle_makes()

makes is a list of typed vehicle make objects.

Getting Vehicle Models

Retrieve models for a specific make and year:

models = client.get_vehicle_models(
    make_id={VEHICLE_MAKE_ID},
    year={VEHICLE_YEAR},
)

Replace {VEHICLE_MAKE_ID} and {VEHICLE_YEAR} with the desired values.

Getting Vehicle Trims

Retrieve trims for a model and year:

trims = client.get_vehicle_trims(
    model_id={VEHICLE_MODEL_ID},
    year={VEHICLE_YEAR},
)

You can also retrieve a trim from its Vehicle Identification Number (VIN):

trim = client.get_vehicle_trim_by_vin(vin={VEHICLE_VIN})

For more detail, see the vehicle make, model, and trim resource documentation in the navigation.

Getting Packages

Retrieve packages for an insurance company and policy type:

from covertier import InsuranceCompany, PolicyType

packages = client.get_packages(
    insurance_company=InsuranceCompany.QUALITAS,
    policy_type=PolicyType.MX_TOURIST,
)

For package contents and available coverages, see the Policy Package resource.

Generating Tourist Quotes

Request and response bodies are typed models. Python date values are serialized to the format expected by the API.

from datetime import date, timedelta

from covertier import InsuranceCompany
from covertier.models import (
    CreateTouristPolicyQuoteRequestBody,
    TouristPolicyQuoteRequestPackage,
    TouristPolicyQuoteRequestVehicle,
)

start_date = date.today()
quote_data = CreateTouristPolicyQuoteRequestBody(
    insurance_company_id=InsuranceCompany.QUALITAS,
    start_date=start_date,
    end_date=start_date + timedelta(days=365),
    package=TouristPolicyQuoteRequestPackage(id={PACKAGE_ID}),
    vehicle=TouristPolicyQuoteRequestVehicle(trim_id={TRIM_ID}),
)

quote = client.create_tourist_policy_quote(quote_data)

Replace {PACKAGE_ID} and {TRIM_ID} with the selected package and trim IDs. If a trim ID is unavailable, provide the vehicle's value and year instead:

vehicle = TouristPolicyQuoteRequestVehicle(
    value={VEHICLE_VALUE},
    year={VEHICLE_YEAR},
)

Issuing Tourist Policies

The following example issues a tourist policy for a holder with a US address:

from datetime import date, timedelta

from covertier import InsuranceCompany
from covertier.models import (
    CreateTouristPolicyRequestBody,
    TouristPolicyRequestAddressUs,
    TouristPolicyRequestPackage,
    TouristPolicyRequestPolicyHolder,
    TouristPolicyRequestVehicle,
)

start_date = date.today()
policy_data = CreateTouristPolicyRequestBody(
    insurance_company_id=InsuranceCompany.QUALITAS,
    start_date=start_date,
    end_date=start_date + timedelta(days=365),
    package=TouristPolicyRequestPackage(id={PACKAGE_ID}),
    vehicle=TouristPolicyRequestVehicle(
        trim_id={TRIM_ID},
        description={VEHICLE_DESCRIPTION},
        vin={VEHICLE_VIN},
    ),
    policy_holder=TouristPolicyRequestPolicyHolder(
        first_name={FIRST_NAME},
        last_name={LAST_NAME},
        birthdate={BIRTHDATE},
        address_us=TouristPolicyRequestAddressUs(
            line1={LINE_1},
            district={DISTRICT},
            city={CITY},
            zip_code={ZIP_CODE},
            state={STATE},
        ),
        mobile_number={MOBILE_NUMBER},
        personal_id_type={PERSONAL_ID_TYPE},
        identification_number={IDENTIFICATION_NUMBER},
    ),
    is_multi_entry=False,
)

policy = client.create_tourist_policy(policy_data)

For a Mexican address, use TouristPolicyRequestAddressMx and assign it to address_mx. Set residence_type_in_mexico on the policy holder as required by the API.

As with quotes, you can provide value and year instead of trim_id in TouristPolicyRequestVehicle. Issuing a policy creates a real insurance contract, so the SDK does not automatically retry this request.

Getting Policy Documents

Retrieve the PDF document links for a policy:

from covertier import InsuranceCompany

document = client.get_policy_document(
    insurance_company=InsuranceCompany.QUALITAS,
    policy_number={POLICY_NUMBER},
)

Handling Errors

API errors expose the response status, headers, and parsed or raw response body:

from covertier import ApiException

try:
    makes = client.get_vehicle_makes()
except ApiException as error:
    print(error.status, error.headers, error.data or error.body)

Using the SDK with Flask

Create one client for the application and close it when the process exits:

import atexit
import os

from flask import Flask, jsonify

from covertier import Client, Environment

app = Flask(__name__)
client = Client(
    access_token=os.environ["COVERTIER_ACCESS_TOKEN"],
    environment=Environment.SANDBOX,
)
atexit.register(client.close)


@app.get("/vehicle-makes")
def vehicle_makes():
    makes = client.get_vehicle_makes()
    return jsonify([make.to_dict() for make in makes])

Was this page helpful?