Forge Documentation

Python API Reference

ForgeCAD exposes a WebSocket JSON-RPC API. The Python package wraps this API — you can also call it directly from any language.

Authentication

All API connections require an API key. Set it as an environment variable or pass it in the connection URL:

# Environment variable (recommended)
export FORGE_API_KEY=fsk_live_xxxxxxxxxxxx

# Or in the Python client
import forgecad as fc
fc.connect(api_key="fsk_live_xxxxxxxxxxxx")

API keys are scoped to a workspace. Generate them in the customer portal under Settings → API Keys.

WebSocket protocol

The API uses JSON-RPC 2.0 over WebSocket on port 8765. Every request has the form:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "document.create",
  "params": { "title": "My Model" }
}

Responses:

// Success
{ "jsonrpc": "2.0", "id": 1, "result": { "id": "doc_abc123" } }

// Error
{ "jsonrpc": "2.0", "id": 1, "error": { "code": -32602, "message": "Invalid params" } }

Event notifications (from events.subscribe) are server-initiated messages with no id field:

{ "jsonrpc": "2.0", "method": "notify", "params": { "event": "document.modified", "id": "doc_abc123" } }

Python client quickstart

import forgecad as fc

# Connects to localhost:8765 by default
# Set FORGE_ENDPOINT to point at a remote server
doc = fc.Document()

# All API methods are available as Python methods
box = doc.add_box(width=50, depth=30, height=20)
box.fillet(radius=2)

props = doc.mass_properties()
print(f"Volume: {props.volume:.2f} mm³")

doc.export("output.step")

Method index

Document

MethodParamsReturnsDescription
document.create{ title?: string }DocumentRefCreate a new empty document.
document.open{ path: string }DocumentRefOpen a .sfc or STEP/IGES file from disk.
document.save{ id: string, path?: string }voidSave the document. If path is omitted, overwrites the source file.
document.export{ id: string, path: string, format: string }voidExport to STEP, IGES, DXF, PDF, or SVG.
document.close{ id: string }voidClose and release a document.

Parameters

MethodParamsReturnsDescription
params.set{ id: string, name: string, value: number }voidSet a named parameter. Triggers a rebuild if the document is parametric.
params.get{ id: string, name: string }numberGet the current value of a named parameter.
params.list{ id: string }Param[]List all parameters and their current values.

Geometry

MethodParamsReturnsDescription
geometry.add_box{ id: string, width: number, depth: number, height: number }FeatureRefAdd a parametric box.
geometry.add_cylinder{ id: string, radius: number, height: number }FeatureRefAdd a parametric cylinder.
geometry.add_sphere{ id: string, radius: number }FeatureRefAdd a sphere.
geometry.boolean_union{ id: string, a: string, b: string }FeatureRefBoolean union of two bodies.
geometry.boolean_cut{ id: string, base: string, tool: string }FeatureRefSubtract tool from base.
geometry.boolean_intersect{ id: string, a: string, b: string }FeatureRefIntersection of two bodies.
geometry.fillet{ id: string, feature: string, radius: number, edges?: string[] }voidApply a fillet. If edges is omitted, all edges are filleted.
geometry.chamfer{ id: string, feature: string, distance: number, edges?: string[] }voidApply a chamfer.
geometry.shell{ id: string, feature: string, thickness: number, open_faces?: string[] }voidShell a solid body.

Analysis

MethodParamsReturnsDescription
analysis.mass_properties{ id: string, feature?: string }MassPropertiesCompute volume, surface area, centre of mass, and moments of inertia.
analysis.bounding_box{ id: string, feature?: string }BoundingBoxAxis-aligned bounding box.
analysis.interference{ id: string, a: string, b: string }InterferenceResultCheck whether two bodies interfere.

Events

MethodParamsReturnsDescription
events.subscribe{ events: string[] }voidSubscribe to document events over the WebSocket connection.
events.unsubscribe{ events: string[] }voidUnsubscribe from events.

Error codes

CodeMeaning
-32700Parse error — invalid JSON
-32600Invalid request
-32601Method not found
-32602Invalid params
-32603Internal error
-32001Document not found
-32002Feature not found
-32003Boolean operation failed — geometry may be degenerate
-32004Rebuild failed — check parameter constraints
-32010Licence error — feature requires Studio or Pro

The complete 413-line API reference (including CAM, FEA, drawing, assembly, and AI methods) is available in the ForgeCAD repository at forge_docs/api_reference.md. The methods above cover the core geometry and document operations.