Skip to main content

OnshapeClient

The core client that handles authentication and provides access to all API resources.

from onshape_sdk import OnshapeClient

Constructor

OnshapeClient(access_key: str, secret_key: str, base_url: str = "https://cad.onshape.com")
ParameterTypeDefaultDescription
access_keystrrequiredOnshape API access key
secret_keystrrequiredOnshape API secret key
base_urlstr"https://cad.onshape.com"Base URL (change for Enterprise)

Class Methods

from_env

OnshapeClient.from_env(
base_url: str = "https://cad.onshape.com",
dotenv_path: str | None = None
) -> OnshapeClient

Creates a client using environment variables ONSHAPE_ACCESS_KEY and ONSHAPE_SECRET_KEY. Automatically loads .env files via python-dotenv.

# Uses .env in current directory
client = OnshapeClient.from_env()

# Custom .env path
client = OnshapeClient.from_env(dotenv_path="/path/to/.env")

# Enterprise instance
client = OnshapeClient.from_env(base_url="https://mycompany.onshape.com")

Instance Methods

test_auth

client.test_auth() -> dict

Runs a diagnostic against two endpoints to verify credentials are valid.

result = client.test_auth()
print(result)

Response:

{
"user": "Zach Sharma (zach@example.com)",
"documents_accessible": 42
}

get

client.get(path: str, query_params: dict = None, accept: str = "application/json") -> requests.Response

Authenticated GET request. Returns the raw requests.Response.

resp = client.get("/api/v6/documents", {"limit": 5})
data = resp.json()

post

client.post(path: str, body: dict = None, query_params: dict = None) -> requests.Response

Authenticated POST with JSON body.

put

client.put(path: str, body: dict = None, query_params: dict = None) -> requests.Response

Authenticated PUT with JSON body.

delete

client.delete(path: str, query_params: dict = None) -> requests.Response

Authenticated DELETE request.

Resource Properties

All resource APIs are lazy-loaded via @cached_property:

PropertyTypeDescription
client.documentsDocumentsApiDocument CRUD, search, versions
client.partsPartsApiPart listing, mass properties, bounding boxes
client.part_studiosPartStudiosApiFeatures, variables, STL/DXF export
client.assembliesAssembliesApiAssembly definition, BOM, mates
client.translationsTranslationsApiAsync CAD export/import
client.drawingsDrawingsApiDrawing views and geometry
client.blob_elementsBlobElementsApiFile upload/download
client.metadataMetadataApiCustom metadata properties
client.thumbnailsThumbnailsApiThumbnail images
client.webhooksWebhooksApiWebhook management
# All resource APIs are accessed as properties
parts = client.parts.list(did, "w", wid, eid)
doc = client.documents.get(did)
bom = client.assemblies.get_bom(did, "w", wid, eid)