Create templates with AI by API
Beta
This feature is in beta. For now it is available only to some users. We will release it gradually to more accounts once testing ends. If you want to use it, ask your JAAK contact for access.
Description
The AI assistant proposes a template from a PDF. A session stores the proposal. You can ask for changes by message. When the proposal is ready, you save the session as a template.
The flow has five steps:
- Create the session with the PDF.
- Read the proposal.
- Send messages to adjust it. You can repeat this step.
- Retry the proposal, if needed.
- Save the session as a template.
All routes are under https://<host-api>/api/v1/templates/. Use the company token in the Authorization: Bearer <token> header.
For the panel version, see Create templates with AI from a PDF.
Route summary
| Method | Path | What it does | Success response |
|---|---|---|---|
POST | /ai-sessions | Creates a session with a PDF. | 201 with the session. |
GET | /ai-sessions/{sessionId} | Reads the session and the proposal. | 200 with the session. |
GET | /ai-sessions/{sessionId}/pdf | Downloads the session PDF. | 200 with application/pdf. |
POST | /ai-sessions/{sessionId}/messages | Sends an instruction to adjust the proposal. | 200 with the session. |
POST | /ai-sessions/{sessionId}/retry | Generates the proposal again. | 200 with the session. |
POST | /ai-sessions/{sessionId}/save | Saves the session as a template. | 201 with template_id. |
1. Create the session
Send a multipart/form-data form:
| Field | Required | Description |
|---|---|---|
file | Yes | The PDF file. |
name | No | Session name. |
instructions | No | Initial instructions for the assistant. |
curl -X POST https://<host-api>/api/v1/templates/ai-sessions \
-H "Authorization: Bearer $TOKEN" \
-F "file=@contrato-demo.pdf;type=application/pdf" \
-F "name=Demo service contract" \
-F "instructions=Mark one signature field for each party."
If the file field is missing, or the file is empty, the response is 400 with the text se requiere un archivo PDF en el campo file. This message is in Spanish.
Session shape
All session responses use this shape. Field names use snake_case.
{
"session_id": "sess_demo_001",
"signers": ["EL PRESTADOR", "EL CLIENTE"],
"fields": [
{
"id": "f1",
"role": "EL CLIENTE",
"type": "signature",
"name": "Client signature",
"required": true,
"x": 72.0,
"y": 640.0,
"w": 180.0,
"h": 60.0,
"page": 2
}
],
"placement": "",
"initials": false,
"uses_generated_pdf": false,
"messages": [
{ "role": "user", "text": "Mark one signature field for each party." },
{ "role": "assistant", "text": "I proposed two signatures on page 2." }
],
"question": {
"text": "Do you want to add initials?",
"options": ["Yes", "No"],
"suggested": "No"
},
"status": "...",
"page_count": 2
}
fieldsholds the proposed fields.x,y,wandhare in PDF points.pagestarts at 1.questionappears when the assistant needs an answer. Send the answer with the messages endpoint.- The assistant service sets
status. Read the value. Do not assume it. uses_generated_pdftells whether the proposal uses a PDF that the assistant generated.
2. Read the session and the PDF
curl https://<host-api>/api/v1/templates/ai-sessions/sess_demo_001 \
-H "Authorization: Bearer $TOKEN"
curl https://<host-api>/api/v1/templates/ai-sessions/sess_demo_001/pdf \
-H "Authorization: Bearer $TOKEN" \
-o demo-session.pdf
The PDF response has the X-Generated header. Its value is true when the assistant generated the PDF, and false when it returns the original.
3. Send a message
curl -X POST https://<host-api>/api/v1/templates/ai-sessions/sess_demo_001/messages \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{ "text": "Move the client signature field to page 3." }'
The body is JSON with a text field. The response is 200 with the updated session.
4. Retry
curl -X POST https://<host-api>/api/v1/templates/ai-sessions/sess_demo_001/retry \
-H "Authorization: Bearer $TOKEN"
Use retry when the proposal fails or you want a new generation. The response is 200 with the session.
5. Save as a template
curl -X POST https://<host-api>/api/v1/templates/ai-sessions/sess_demo_001/save \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"folder_name": "Demo contracts",
"signature_type": "simple"
}'
| Field | Description |
|---|---|
folder_name | Folder where the template is stored. |
signature_type | Signature type of the template. See Signature types. |
201 response:
{ "template_id": 1234 }
The template_id is the ID of the new template. Use that ID in Send with template values by API.
To change the signature type of each role after saving, see Signature type per signer.
Errors
| Response | Cause | What to do |
|---|---|---|
400 se requiere un archivo PDF en el campo file | The file field is missing or the file is empty. | Send a PDF in the file field. |
400 cuerpo de la solicitud inválido | The JSON body of messages or save is not valid. | Check the JSON. |
422 | The PDF has no text layer (NO_TEXT_LAYER). The assistant cannot read it. | Use a PDF with selectable text. |
409 | A validation of the assistant failed (GUARD_FAILED). | Check the session. Ask for another change or retry. |
| Other code | Error from the assistant service. The response text says what failed. | Retry. If it continues, contact JAAK. |
Limits
- The gateway code does not set a maximum PDF size. Confirm the limit with your JAAK contact.
- The gateway forwards
signature_typeto the assistant service without checking it. Use a type from the Signature types table. The final check is done by the template service. This repository does not show it.