Skip to main content

mc-sdk-python

Official Python SDK for the Monte Carlo REST API.

The SDK is one package, pycarlo2. Most of it is generated from the API's OpenAPI spec. Client construction and credentials are hand-written, so every request is authenticated, and so is the helper that walks a paginated list.

For the GraphQL API, use pycarlo instead. This SDK targets the REST API.

The SDK reference has a page for every module, operation group and model.

Install

pip install pycarlo2

Requires Python 3.10 or later.

Usage

The endpoint defaults to pycarlo2.auth.DEFAULT_ENDPOINT, https://api.getmontecarlo.com, which serves every instance. Pass endpoint only to reach a different one.

With a Monte Carlo API token, supply the two halves separately and the SDK forms the credential:

import os

import pycarlo2

client = pycarlo2.new_client(
    pycarlo2.Options(
        endpoint="https://api.getmontecarlo.com",
        token_id=os.environ["MCD_ID"],
        token_secret=os.environ["MCD_TOKEN"],
    )
)
me = pycarlo2.UsersApi(client).get_current_user()

With OAuth 2.0 client credentials, where instance selects the deployment the gateway routes to:

client = pycarlo2.new_client(
    pycarlo2.Options(
        endpoint="https://api.getmontecarlo.com",
        client_id=os.environ["MCD_CLIENT_ID"],
        client_secret=os.environ["MCD_CLIENT_SECRET"],
        instance="us1",
    )
)

Tokens are fetched and renewed as needed, so a long-lived client does not go stale. Neither the token exchange nor the API client follows a redirect.

The operations and the request and response models they take are all importable from the package, so one import covers everything: pycarlo2.DeploymentIn, pycarlo2.ProblemOut, and so on. Errors from the API raise pycarlo2.ApiException; a misconfiguration raises pycarlo2.CredentialsError when the client is built.

Walking a paginated list

A list that pages returns one page per call, with next_cursor to ask for the next. paginate follows the cursor for you and yields every item, so nothing has to track it:

from pycarlo2 import WarehousesApi, new_client
from pycarlo2.paging import paginate

warehouses = WarehousesApi(new_client(options))

for warehouse in paginate(warehouses.list_warehouses):
    print(warehouse.id, warehouse.name)

Any other keyword arguments go to every call, so a filter a list accepts applies to the whole walk, except with_count, which paginate drops, since a walk yields every item and never reports a total. Each call asks for the largest page the API serves; passing limit overrides it.

A failed request raises whatever the client raises, so a walk ends on an ApiException like any other call. A page that promises more items without a cursor to ask for, or that repeats a cursor already served, raises NoCursorError or RepeatedCursorError from the same module; both subclass PaginationError. These guards catch a server that repeats a cursor, not one that mints a fresh cursor forever, so bound a walk of unknown length yourself with itertools.islice or a timeout passed among the keyword arguments. Call the list method directly when you want one page, the cursor, or a total.

paginate is one function for every paginated list, because a page is recognised by its attributes. The Go SDK generates an All method per list instead.

Using credentials you have already configured

If you have configured the Monte Carlo CLI, the SDK reads the same mcd_id and mcd_token from ~/.mcd/profiles.ini, so a client needs no options at all:

client = pycarlo2.new_client()

If your profile has an mcd_api_endpoint key, set when the CLI or pycarlo point at a non-default deployment, this SDK reads it too but reinterprets it: there it holds the GraphQL endpoint (.../graphql), and this SDK strips the trailing /graphql before using it as the REST base URL.

Resolution picks a whole credential mechanism, not individual fields. If Options supplies a complete mechanism, the environment and the profile are not consulted for credentials. Otherwise: MCD_DEFAULT_OAUTH_CLIENT_ID and MCD_DEFAULT_OAUTH_CLIENT_SECRET (with MCD_DEFAULT_INSTANCE_ID) are tried first, then MCD_DEFAULT_API_ID and MCD_DEFAULT_API_TOKEN, then a profile from ~/.mcd/profiles.ini, which the CLI writes. A profile supplies instance and the token endpoint only when it also supplied the OAuth credentials; an endpoint left empty is always filled from the profile.

A pair is all or nothing. If you pass one half of an API token or of OAuth client credentials, that is an error; the other half is never filled in from the environment or a profile.

This SDK does not read MCD_API_ENDPOINT, even though the CLI and pycarlo both do. That variable carries the GraphQL endpoint, and endpoint here is the REST base URL.

To pick a profile other than default, set profile, or the MCD_DEFAULT_PROFILE environment variable. A profile you name in profile has to exist; the SDK reports that rather than quietly falling back to different credentials. MCD_DEFAULT_PROFILE only selects which section is read when a profile is consulted at all.

Usage telemetry

Every request carries x-mcd-telemetry-reason: user and x-mcd-telemetry-service: mc-sdk-python, the headers pycarlo and the CLI send, so Monte Carlo can tell which client made a call. They carry nothing about its arguments or payload. A tool built on the SDK names itself by overriding them with client.set_default_header(...).

Reference

  • The SDK reference has a page for every module, operation group and model, and guides to setting up credentials.
  • The REST API reference describes each operation's requests and responses.

Contributing

Issues and pull requests are welcome. CONTRIBUTING.md says which files are generated, which can be changed by hand, and how to preview the reference.

Metadata

Release files for pycarlo2 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycarlo2 0.1.4
File Size Uploaded Provenance
pycarlo2-0.1.4.tar.gz 178.6 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pycarlo2 0.1.4
File Interpreter ABI Platform
pycarlo2-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 766.3 kB

Release files / pycarlo2-0.1.4.tar.gz

Download URL pycarlo2-0.1.4.tar.gz
Size 178.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5ae446b277b251ee6f5605671d858d8f93cac0cd9997744392e833a9ad13250d
BLAKE2b-256 checksum
How to use checksums
1e20cd5a60ce3a35583897442012d2b1f1fa3dc146348bf486e74a61f812deff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release files / pycarlo2-0.1.4-py3-none-any.whl

Download URL pycarlo2-0.1.4-py3-none-any.whl
Size 587.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d82f02472893ffb09f2ca0f65b85c9dc2c09bcb0d0bcabb50bcb2939084c621
BLAKE2b-256 checksum
How to use checksums
ccdbcf66a9ee731056794c1b02801681aafc6d1e1226fc60ad553e60bf801e69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Security for pycarlo2 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pycarlo2-0.1.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page