View as Markdown

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.

1. Turn it on in 1 minute

  1. Open ERPSqlite → CAD → AI API button.
  2. Press Enable the API. If the AI runs on another computer of your network, tick Reachable from other computers on the local network.
  3. Press Create key and keep it: it is not shown again.
  4. 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

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:

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.svg

4. Endpoints

MethodPathWhat it does
GET/api/cadCatalogue: endpoints, operations, entity format and every command (no key)
GET/api/cad/openapi.jsonOpenAPI 3.1 description (no key)
GET/api/cad/prompt?lang=enReady-made instructions for the AI (no key)
GET/api/cad/drawingsList of drawings: id, name, dates and number of entities
POST/api/cad/drawingsCreate 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}/opsApply JSON operations and save (a single undo step)
POST/api/cad/drawings/{id}/commandsType 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}/svgSVG preview (to check the result)
GET/api/cad/drawings/{id}/dxfDXF file
GET/api/cad/drawings/{id}/jsonThe 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

6. Entities (ENTITY)

KeyMeaning
tline, pline, circle, arc, text, point or dim
pPoints [[x,y], ...]: line 2; pline its vertices; circle, arc and text the centre or insertion point
lLayer (created if missing)
cACI 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
gTag: the element's name, to select it later
cltrue = closed polyline
rRadius (circle, arc)
a0, a1Arc start and end angles (degrees, counter-clockwise)
xText
hText height
rotText rotation (degrees)
kDimension kind: lin (p = [p1, p2, location], rot 0 horizontal or 90 vertical), ali, rad, dia (p = [centre, location], r), lead (p = points, x = text)
hz3D height (extrusion, like push/pull)
zBase elevation

When reading, every entity also has its id. Architecture entities that are not created with add may appear too:

Walls are created with the floorplan operation or the ARQ* commands.

7. Operations (POST …/ops)

{"ops": [ ... ]} run in order, as a single undo step:

OperationFields
layername, color (ACI), ltype (CONTINUOUS, DASHED, HIDDEN, CENTER, DASHDOT, DOT), lw (mm). Creates or changes the layer
current_layername
addentities: list of ENTITY
deletesel
move / copysel, dx, dy
rotatesel, base [x,y], angle (degrees)
scalesel, base [x,y], factor
mirrorsel, p1, p2, keep (keep the originals)
setsel and what changes: layer, color (null = by layer), tag, text, hz, z
zoom_extentsNo fields
floorplanA whole floor plan (see below)

Selector `sel`

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:

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:

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

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:

CommandAliasesWhat it does
LINELSegments: start point, next points…; C closes, "" ends
PLINEPLPolyline: points…; C closes it
RECTANGRECRectangle: corner, opposite corner (or @w,h, 4000x3000)
POLYGONPOLRegular polygon: sides, centre, I (inscribed) or C (circumscribed), radius
CIRCLECCircle: centre, radius
ARCAArc through three points
POINTPOPoint
TEXTT, DTText: point, height, rotation, text and "" to finish
TRIMTRTrim: "" (everything cuts), a point on the piece to remove…, ""
EXTENDEXExtend up to the boundaries
FILLETFFillet two lines with a radius
ERASEEErase the selection
COPYCO, CPCopy: selection, base point, destination(s)
MOVEMMove: selection, base point, destination
ROTATERORotate: selection, base, angle
SCALESCScale: selection, base, factor
MIRRORMIMirror: selection, two points of the axis, keep
OFFSETOParallel copy: distance, a point on the object, a point on the side, ""
ARRAYARRectangular or polar array
EXPLODEXExplode polylines and blocks
PUSHPULLPP, EXT3D height of closed shapes
DIMLINEARDLIHorizontal or vertical dimension
DIMALIGNEDDALAligned dimension
DIMRADIUS / DIMDIAMETERDRA / DDIRadius / diameter dimension
DIMANGULARDANAngle dimension
LEADERLELeader with text
DIST / AREA / ID / LISTDI / AA / ID / LIMeasure (answer in messages)
UNDO / REDOUUndo / redo
ARQMUROWall along points (thickness and height from the settings)
ARQHABITACIONRectangular room with its walls
ARQPUERTA / ARQVENTANADoor / window: a point on the wall (on the side it opens to), ""
ARQPISOFloor slab
ARQBLOQUEFurniture 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": { ... }}

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": "..."}}
CodeMeaning
400Bad request: invalid JSON, empty name, invalid units…
401The key is missing or not valid
403The key's user has no access to the CAD, or can only read
404The drawing does not exist
405Method 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.