SDKs

Python SDK

levelfour is the Python client for the LevelFour API, in a sync form and an async one. Everything here is about the client itself. What each method takes and returns lives on the reference page for its sub-client, listed below.

Installation

pip install levelfour

Client setup

Sync client

from levelfour import LevelFour

client = LevelFour(
    api_key="l4_live_...",
    base_url="https://api.levelfour.ai",
    timeout=30.0,
    max_retries=2,
)

With LEVELFOUR_API_KEY in the environment, the constructor takes nothing:

client = LevelFour()
Prefer the empty constructor and the environment variable. An api_key written as an argument reaches version control with the file, and it stays in the history after you delete the line. Authentication covers keeping a key out of your source.

LevelFour works as a context manager:

with LevelFour() as client:
    summary = client.recommendations.get_savings_by_provider()

Async client

import asyncio
from levelfour import AsyncLevelFour

async def main():
    async with AsyncLevelFour() as client:
        summary = await client.recommendations.get_savings_by_provider()

asyncio.run(main())

Constructor options

ParameterTypeDefault
api_keystr | NoneLEVELFOUR_API_KEY env var
base_urlstrhttps://api.levelfour.ai
timeoutfloat30.0
max_retriesint2
http_clienthttpx.Client | NoneBuilt-in
headersdict[str, str] | NoneNone
One type changes with the client you built. http_client takes an httpx.Client on LevelFour and an httpx.AsyncClient on AsyncLevelFour.

Cloning a client

with_options returns a copy of the client with the options you name changed:

long_timeout_client = client.with_options(timeout=60.0)

What the client exposes

Every sub-client hangs off the client you just built. The reference pages carry each method with its parameters, its response shape and a worked example.

AttributeReference
client.recommendationsRecommendations
client.recommendations.auditSavings
client.costsCosts
client.providersProviders
client.api_keysAPI keys
client.webhooksWebhooks
client.authAuthentication

Pagination

Methods that return paginated results return a SyncPager, or an AsyncPager on the async client.

Pagination has the iteration forms, sync and async, alongside the response envelope and the page size limit.

Request options

Override the client's defaults for one call:

from levelfour.core.request_options import RequestOptions

opts: RequestOptions = {
    "timeout_in_seconds": 60,
    "max_retries": 5,
    "additional_headers": {"X-Request-Id": "abc123"},
}

summary = client.recommendations.get_savings_by_provider(request_options=opts)
OptionTypeDescription
timeout_in_secondsintOverride request timeout
max_retriesintOverride max retry attempts
additional_headersdict[str, Any]Extra headers for this request
additional_query_parametersdict[str, Any]Extra query parameters
additional_body_parametersdict[str, Any]Extra body parameters

Next

  • Resources has every method with its parameters and response shape, in Python alongside TypeScript and Go
  • Error handling is the exception hierarchy, what each error carries, and which failures the client retries
  • Webhooks is the verifier, the signature headers, the event types and the payloads
  • Authentication covers LEVELFOUR_API_KEY, key scopes, rotation and revocation
  • Quickstart is install, key, first call
  • TypeScript SDK and Go SDK cover the same surface in the other two languages