CAD API for AI
ERPSqlite includes a 2D/3D CAD. This HTTP API lets an AI (or any program) create, read and modify drawings, either with JSON operations (the same ones the CAD assistant uses) or with command-line commands, as if someone were typing them.
- Live catalogue (no key):
GET /api/cad. It is the source of truth: it lists every operation and every command with its aliases. - OpenAPI 3.1 (no key):
GET /api/cad/openapi.json, for ChatGPT GPTs and other agents. - Ready-made AI prompt:
GET /api/cad/prompt?lang=en. - This document as Markdown: https://www.erp-sqlite.xyz/docs/cad-api.en.md
1. Turn it on in 1 minute
- Open ERPSqlite → CAD → AI API button.
- Press Enable the API. If the AI runs on another computer of your network, tick Reachable from other computers on the local network.
- Press Create key and keep it: it is not shown again.
- Press Copy prompt and paste it into your AI as the system instructions (or at the start of the conversation). It already contains the address and the key.
The API is served by the desktop app, on http://127.0.0.1:8169 (the port is changed in Settings → External API).
Which AI to use
- An AI on the same computer (Claude Code, Cursor, an agent with HTTP access): it works directly with
127.0.0.1. - A cloud AI (a ChatGPT GPT with actions, Claude.ai…): it cannot reach
127.0.0.1. Publish the port with a tunnel (Cloudflare Tunnel, ngrok) and use that address. In a GPT, import the schema fromhttps://YOUR-ADDRESS/api/cad/openapi.jsonand set the key as API Key → Bearer authentication.
2. Authentication
On every call (except the catalogue, the OpenAPI and the prompt):
Authorization: Bearer YOUR_KEY
X-API-Key: YOUR_KEY also works. The AI has the permissions of the user who owns the key:
- no access to the CAD app → 403;
- read-only CAD → it can read but not change;
- what it creates or changes is recorded under that user.
Keys are revoked in Settings → External API.
3. First steps with curl
BASE=http://127.0.0.1:8169
KEY=YOUR_KEY
# What the API can do
curl $BASE/api/cad
# Create a drawing with a 4 × 3 m room and its label
curl -X POST $BASE/api/cad/drawings \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"name": "Shed", "units": "mm", "ops": [
{"op": "layer", "name": "Walls", "color": 7},
{"op": "add", "entities": [
{"t": "pline", "p": [[0,0],[4000,0],[4000,3000],[0,3000]], "cl": true, "l": "Walls", "g": "outline"},
{"t": "text", "p": [[1500,1500]], "x": "Shed", "h": 250, "g": "label"}
]}
]}'
# Read it (layers, entities with their id and a description)
curl -H "Authorization: Bearer $KEY" $BASE/api/cad/drawings/1
# Move the label and make the outline red
curl -X POST $BASE/api/cad/drawings/1/ops \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"ops": [
{"op": "move", "sel": {"tags": ["label"]}, "dx": 100, "dy": -100},
{"op": "set", "sel": {"tags": ["outline"]}, "color": 1}
]}'
# With commands: a rectangle and a circle
curl -X POST $BASE/api/cad/drawings/1/commands \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"commands": ["REC", "5000,0", "@2000,1000", "C", "6000,500", "300"]}'
# See the result
curl -H "Authorization: Bearer $KEY" $BASE/api/cad/drawings/1/svg > shed.svg4. Endpoints
| Method | Path | What it does |
|---|---|---|
| GET | /api/cad | Catalogue: endpoints, operations, entity format and every command (no key) |
| GET | /api/cad/openapi.json | OpenAPI 3.1 description (no key) |
| GET | /api/cad/prompt?lang=en | Ready-made instructions for the AI (no key) |
| GET | /api/cad/drawings | List of drawings: id, name, dates and number of entities |
| POST | /api/cad/drawings | Create a drawing. Body: name, units (mm, cm or m), and optionally ops and commands; or file with an exported JSON to import it |
| GET | /api/cad/drawings/{id} | The drawing: units, layers, bounds, entities with id and description (text for the AI) |
| POST | /api/cad/drawings/{id}/ops | Apply JSON operations and save (a single undo step) |
| POST | /api/cad/drawings/{id}/commands | Type command-line inputs and save |
| PATCH | /api/cad/drawings/{id} | Rename: {"name": "..."} |
| DELETE | /api/cad/drawings/{id} | Delete the drawing |
| GET | /api/cad/drawings/{id}/svg | SVG preview (to check the result) |
| GET | /api/cad/drawings/{id}/dxf | DXF file |
| GET | /api/cad/drawings/{id}/json | The whole drawing as a JSON file (export) |
Create, ops and commands return the updated drawing (units, layers, bounds and entities), so the AI does not need to ask for it again.
5. Coordinates and units
- Coordinates are in the drawing units (
units): mm by default, also cm or m. - Y grows upwards.
- Angles are in degrees, counter-clockwise from +X.
boundsis[xmin, ymin, xmax, ymax]of everything drawn.
6. Entities (ENTITY)
| Key | Meaning |
|---|---|
t | line, pline, circle, arc, text, point or dim |
p | Points [[x,y], ...]: line 2; pline its vertices; circle, arc and text the centre or insertion point |
l | Layer (created if missing) |
c | ACI colour (without it, the layer's): 1 red, 2 yellow, 3 green, 4 cyan, 5 blue, 6 magenta, 7 white/black, 8 grey, 30 orange |
g | Tag: the element's name, to select it later |
cl | true = closed polyline |
r | Radius (circle, arc) |
a0, a1 | Arc start and end angles (degrees, counter-clockwise) |
x | Text |
h | Text height |
rot | Text rotation (degrees) |
k | Dimension kind: lin (p = [p1, p2, location], rot 0 horizontal or 90 vertical), ali, rad, dia (p = [centre, location], r), lead (p = points, x = text) |
hz | 3D height (extrusion, like push/pull) |
z | Base elevation |
When reading, every entity also has its id. Architecture entities that are not created with add may appear too:
wall: a wall with axisp, thicknessthandopenings;block: a library furniture block, with itsname.
Walls are created with the floorplan operation or the ARQ* commands.
7. Operations (POST …/ops)
{"ops": [ ... ]} run in order, as a single undo step:
| Operation | Fields |
|---|---|
layer | name, color (ACI), ltype (CONTINUOUS, DASHED, HIDDEN, CENTER, DASHDOT, DOT), lw (mm). Creates or changes the layer |
current_layer | name |
add | entities: list of ENTITY |
delete | sel |
move / copy | sel, dx, dy |
rotate | sel, base [x,y], angle (degrees) |
scale | sel, base [x,y], factor |
mirror | sel, p1, p2, keep (keep the originals) |
set | sel and what changes: layer, color (null = by layer), tag, text, hz, z |
zoom_extents | No fields |
floorplan | A whole floor plan (see below) |
Selector `sel`
{"tags": ["door 1"]}: by tag. The most stable one; a tag also selects tags that start with it followed by a space or a number.{"ids": [0, 3]}: by id. Ids are positions in the list and change after deleting.{"layer": "Walls"}: a whole layer.
Floor plan (floorplan)
Draws walls with their thickness and clean corners, door openings with leaf and swing, windows, room names with m², dimensions and 3D heights:
{"op": "floorplan", "replace": true, "origin": [0, 0],
"exterior_wall": 200, "interior_wall": 100, "height": 2500,
"rooms": [
{"name": "Living room", "x": 0, "y": 0, "w": 5000, "h": 4000},
{"name": "Bedroom", "x": 5000, "y": 0, "w": 3500, "h": 4000},
{"name": "Porch", "x": 0, "y": -2000, "w": 5000, "h": 2000, "type": "porch"}
],
"doors": [
{"room": "Living room", "wall": "south", "at": 0.5, "width": 900},
{"room": "Bedroom", "wall": "west", "at": 0.5, "width": 800}
],
"windows": [{"room": "Bedroom", "wall": "east", "at": 0.5, "width": 1200}],
"roof": {"thickness": 200}}floorplan rules:
- Rooms are rectangles (x, y = south-west corner; w, h = size) whose edges are the wall axes. They do not overlap; neighbours share their common edge exactly.
wall:north,south,eastorwest.atgoes from 0 to 1 along that wall, from the west or from the south.- To change the plan, send it whole again with
"replace": true.
8. Commands (POST …/commands)
{"commands": ["L", "0,0", "1000,0", "@0,500", "C"]}: each item is what would be typed in the command line before pressing Enter. "" is an empty Enter; it ends the command or accepts the proposed value.
How points are given:
x,y: absolute;@dx,dy: relative to the last point;@distance<angleordistance<angle;4000x3000: size of a rectangle;- a number alone: distance in the current direction.
Numbers accept arithmetic (34*67/89+67). Options are answered with their letter or word (e.g. C to close).
Picking objects without a mouse
- When a command asks you to pick an object (TRIM, FILLET, OFFSET, ARQPUERTA…), type a point that lies on it:
x,y. It works within about 20 mm. - When it asks for a selection (MOVE, COPY, ROTATE…), each point adds the object at that place;
ALLselects everything and""ends the selection.
Repeating commands
LINE, PLINE, ARQMURO, ARQPUERTA, ARQVENTANA and TRIM keep asking until an Enter. In a list with several commands, end each one with "" before the next one:
{"commands": [
"ARQHABITACION", "0,0", "4000,3000",
"ARQPUERTA", "2000,0", "",
"ARQVENTANA", "4000,1500", "",
"L", "6000,0", "9000,0", "",
"TR", "", "8500,0", ""
]}If the list ends with one of these commands waiting for another point, the API presses that last Enter for you.
The response includes:
messages: what the CAD wrote in its history (DIST, AREA, LIST measurements…);added: new entities;pending_command: if a command was left unfinished (a point or a value was missing), it is cancelled and named here.
| Command | Aliases | What it does |
|---|---|---|
| LINE | L | Segments: start point, next points…; C closes, "" ends |
| PLINE | PL | Polyline: points…; C closes it |
| RECTANG | REC | Rectangle: corner, opposite corner (or @w,h, 4000x3000) |
| POLYGON | POL | Regular polygon: sides, centre, I (inscribed) or C (circumscribed), radius |
| CIRCLE | C | Circle: centre, radius |
| ARC | A | Arc through three points |
| POINT | PO | Point |
| TEXT | T, DT | Text: point, height, rotation, text and "" to finish |
| TRIM | TR | Trim: "" (everything cuts), a point on the piece to remove…, "" |
| EXTEND | EX | Extend up to the boundaries |
| FILLET | F | Fillet two lines with a radius |
| ERASE | E | Erase the selection |
| COPY | CO, CP | Copy: selection, base point, destination(s) |
| MOVE | M | Move: selection, base point, destination |
| ROTATE | RO | Rotate: selection, base, angle |
| SCALE | SC | Scale: selection, base, factor |
| MIRROR | MI | Mirror: selection, two points of the axis, keep |
| OFFSET | O | Parallel copy: distance, a point on the object, a point on the side, "" |
| ARRAY | AR | Rectangular or polar array |
| EXPLODE | X | Explode polylines and blocks |
| PUSHPULL | PP, EXT | 3D height of closed shapes |
| DIMLINEAR | DLI | Horizontal or vertical dimension |
| DIMALIGNED | DAL | Aligned dimension |
| DIMRADIUS / DIMDIAMETER | DRA / DDI | Radius / diameter dimension |
| DIMANGULAR | DAN | Angle dimension |
| LEADER | LE | Leader with text |
| DIST / AREA / ID / LIST | DI / AA / ID / LI | Measure (answer in messages) |
| UNDO / REDO | U | Undo / redo |
| ARQMURO | Wall along points (thickness and height from the settings) | |
| ARQHABITACION | Rectangular room with its walls | |
| ARQPUERTA / ARQVENTANA | Door / window: a point on the wall (on the side it opens to), "" | |
| ARQPISO | Floor slab | |
| ARQBLOQUE | Furniture block from the library | |
| EST* | Structures: bars, columns, sections, supports and loads |
The full, always up-to-date list is in GET /api/cad, under commands.
9. Export and import as JSON
The whole drawing (layers, entities, dimension styles, floor plan, budget…) is saved in a JSON file:
{"format": "erpsqlite-cad", "version": 1, "name": "Shed", "units": "mm",
"exported": "2026-10-22T18:00:00", "drawing": { ... }}- In the app: CAD → a drawing's menu → Download JSON, or inside the drawing File → Save as JSON…. To import it, Open DXF/DWG/JSON.
- Through the API:
GET /api/cad/drawings/{id}/jsonexports it andPOST /api/cad/drawingswith{"file": <the JSON>}imports it as a new drawing, with its name (or the one given inname).
Use it for backups, to move drawings between databases or computers, and to let an AI save and reload a whole drawing. The full file specification is in CAD drawing JSON format.
10. Errors
Errors answer with their HTTP code and:
{"error": {"name": "...", "message": "..."}}| Code | Meaning |
|---|---|
| 400 | Bad request: invalid JSON, empty name, invalid units… |
| 401 | The key is missing or not valid |
| 403 | The key's user has no access to the CAD, or can only read |
| 404 | The drawing does not exist |
| 405 | Method not allowed on that path |
11. AI prompt
Copy this as your AI's system instructions. Change the base URL and the key, or use the CAD's Copy prompt button, which fills them in.
You are an expert CAD draughtsperson. You draw in the ERPSqlite CAD through its HTTP API.
CONNECTION
- Base URL: http://127.0.0.1:8169 (every path starts with /api/cad)
- Send this header on every call: Authorization: Bearer <API_KEY>
- Request and response bodies are JSON (Content-Type: application/json).
- Full reference: https://www.erp-sqlite.xyz/docs/cad-api.en.md (OpenAPI: http://127.0.0.1:8169/api/cad/openapi.json)
BEFORE DRAWING
1. GET /api/cad: the catalogue with the operations, the entity format and every command with its aliases. Use it as the source of truth; never invent operations or commands.
2. GET /api/cad/drawings: the existing drawings. If the user means a specific drawing, find it by name; otherwise create one with POST /api/cad/drawings {"name": "...", "units": "mm"}.
3. GET /api/cad/drawings/{id}: units, layers, bounds and entities with their "id". ALWAYS read it before changing anything.
HOW TO DRAW (in this order of preference)
- Houses, flats, offices: ONE "floorplan" operation (rooms are rectangles sharing their edges exactly, a door in every room, windows on exterior walls). To change it, send the whole floorplan again with "replace": true.
- General geometry: POST /api/cad/drawings/{id}/ops with "add" (line, pline, circle, arc, text, point, dim).
- Command-line commands (POST /api/cad/drawings/{id}/commands) for what the operations do not cover (POLYGON, dimensions such as DIMLINEAR, ARQMURO, ARQHABITACION, ARQPUERTA, ARQVENTANA, TRIM, FILLET, OFFSET...). Each list item is one input; "" = Enter. When a command asks you to pick an object, type a point that lies on it ("x,y"). Repeating commands (LINE, ARQPUERTA, ARQVENTANA, TRIM...) end with "" before the next command.
- To change things use move, copy, rotate, scale, mirror, set or delete with "sel" by "tags", "ids" or "layer". Never redraw what already exists.
RULES
- Use the drawing units ("units", mm by default) and convert what the user says (3 m = 3000 mm). Y grows upwards; angles in degrees, counter-clockwise.
- Organise the drawing in clearly named layers (Walls, Doors, Windows, Dimensions, Text, Furniture) and create each layer with the "layer" operation before using it.
- Give every element a meaningful tag "g" ("living room window", "table 1") so you can select it later; prefer selecting by tag.
- "id"s are positions in the list and change after deleting: read the drawing again after a delete.
- Dimension the main sizes (dim "lin" with rot 0 or 90).
- Send several operations in one call: they run in order as a single undo step.
- After every change check the response (it returns the updated drawing) and, if you can see images, GET /api/cad/drawings/{id}/svg to verify the result. Fix overlaps, gaps or wrong sizes before you finish.
- If a commands call returns "pending_command", that command was left unfinished: check your inputs.
- If the API answers {"error": {...}}, read the message, fix the request and try again; do not repeat the same call.
- Never delete drawings or objects the user did not ask to delete.
WHEN YOU FINISH
Tell the user, in their language and in 2-4 lines, what you drew (drawing id and name, main sizes) and how to see it: ERPSqlite → CAD → open the drawing.