Logo
GenerationDraft Generator

Create Draft

Overview

The Create Draft endpoint generates a new document by applying user-provided values to a template extracted via the Generate Template API. It takes the template form fields with customizations and produces a formatted HTML document with all substitutions applied.

Use this endpoint to:

  • Generate customized documents from templates
  • Create multiple variations of contracts and agreements
  • Automate document generation with variable substitution
  • Produce formatted, ready-to-use HTML documents
  • Build document automation and generation workflows

Endpoint Details

Method: POST
Endpoint: /api/agent/draft/create_draft
Base URL: https://api.k-v.ai
Authentication: Access Key (Required)

Critical: The template_doc_process_id must match the document ID used in the Generate Template API.


Request Specification

Headers

HeaderTypeRequiredDescription
access-keystringYesYour unique access-key generated from the platform UI
Content-TypestringYesMust be application/json

Request Body

{
    "template_doc_process_id": "264dfa262b748d15ccbeaada89430c68",
    "model_data": {
        "model_name": "gpt-5.1",
        "api_key": ""
    },
    "filled_template_variables": [
        {
            "category": "Agreement Basic Information",
            "label": "Governing Law State",
            "type": "text",
            "description": "State whose laws govern the Settlement Agreement",
            "current_value": [
                "California"
            ],
            "options": null,
            "page_no": null,
            "user_value": "New York"
        },
        {
            "category": "Party Information - Plaintiff",
            "label": "Plaintiff Name",
            "type": "text",
            "description": "Name of the Plaintiff entity",
            "current_value": [
                "ABC Software Corporation"
            ],
            "options": null,
            "page_no": null,
            "user_value": "XYZ Software Corporation"
        }
    ]
}

Body Fields

FieldTypeRequiredDescription
template_doc_process_idstringYesDocument ID of the template (must match the doc_process_id used in Generate Template API)
model_dataobjectYesAI model configuration
model_data.model_namestringYesAI model to use for draft generation (see supported models below)
model_data.api_keystringNoYour own LLM API key (leave empty to use platform's default keys)
filled_template_variablesarrayYesArray of template fields with user values (obtained from Generate Template API response)

Understanding user_value

The user_value field determines what appears in the generated draft:

user_valueBehaviorExample
nullKeeps current_value from template"user_value": null → Uses "California"
Any valueReplaces with your custom value"user_value": "New York" → Uses "New York"

Example:

{
    "label": "Governing Law State",
    "current_value": ["California"],
    "user_value": "New York"  // This value will appear in the final draft
}

Supported AI Models

Model NameProvider
gpt-5.1OpenAI
gpt-5-miniOpenAI
claude-sonnet-4-5-20250929Anthropic
gemini/gemini-2.5-flash-liteGoogle
gemini/gemini-2.5-proGoogle
gemini/gemini-3-pro-previewGoogle
mistral/mistral-small-latestMistral AI
mistral/mistral-medium-latestMistral AI
llama3.1-70bMeta

Using Your Own LLM API Keys

Platform Keys (Default)

{
    "model_data": {
        "model_name": "gpt-5.1",
        "api_key": ""
    }
}

Your Own Keys

{
    "model_data": {
        "model_name": "gpt-5.1",
        "api_key": "sk-your-openai-api-key-here"
    }
}

Response Specification

Success Response (200 OK)

{
    "data": "<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n<body>\n<div id=\"page-1\">\n<p><b>SETTLEMENT AGREEMENT AND RELEASE</b></p>\n<p>THIS SETTLEMENT AGREEMENT is entered into between XYZ Software Corporation (Plaintiff), and Universal Corporation (Defendant).</p>\n<p>...governed by the laws of the State of New York...</p>\n</body>\n</html>",
    "message": "Draft created successfully"
}

Response Fields

FieldTypeDescription
datastringComplete HTML document with applied changes and formatting
messagestringHuman-readable response message

Error Responses

401 Unauthorized

{
    "data": {},
    "message": "Invalid or missing access key"
}

Cause: Missing or invalid access-key header.


422 Unprocessable Entity - Invalid Model Name

{
    "detail": [
        {
            "type": "literal_error",
            "loc": [
                "body",
                "model_data",
                "model_name"
            ],
            "msg": "Input should be 'gpt-5-chat-latest', 'gpt-5.1', 'gpt-5-mini', 'claude-sonnet-4-5-20250929', 'gemini/gemini-2.5-flash-lite', 'mistral/mistral-small-latest', 'mistral/mistral-medium-latest', 'gemini/gemini-2.5-pro', 'gemini/gemini-3-pro-preview' or 'llama3.1-70b'",
            "input": "",
            "ctx": {
                "expected": "'gpt-5-chat-latest', 'gpt-5.1', 'gpt-5-mini', 'claude-sonnet-4-5-20250929', 'gemini/gemini-2.5-flash-lite', 'mistral/mistral-small-latest', 'mistral/mistral-medium-latest', 'gemini/gemini-2.5-pro', 'gemini/gemini-3-pro-preview' or 'llama3.1-70b'"
            }
        }
    ]
}

Cause: Invalid or unsupported model_name in model_data. See supported models list above.


400 Bad Request - Invalid Document IDs

{
    "data": {},
    "message": "Invalid docs selected"
}

Causes:

  • Missing or invalid doc_process_ids.

500 Internal Server Error - Invalid LLM API Key

{
    "data": {},
    "message": "litellm.AuthenticationError: AuthenticationError: OpenAIException - Incorrect API key provided: tyrdfuih**uhf7. You can find your API key at https://platform.openai.com/account/api-keys."
}

Cause: The api_key provided in model_data is invalid or expired. Verify your LLM provider API key.


500 Internal Server Error - General Error

{
    "data": {},
    "message": "Something went wrong"
}

Causes:

  • LLM service temporarily unavailable
  • Server-side processing error

Code Snippets

curl --location 'https://api.k-v.ai/api/agent/draft/create_draft' \
--header 'access-key: YOUR_ACCESS_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "template_doc_process_id": "264dfa262b748d15ccbeaada89430c68",
    "model_data": {
        "model_name": "gpt-5.1",
        "api_key": ""
    },
    "filled_template_variables": [
        {
            "category": "Agreement Basic Information",
            "label": "Governing Law State",
            "type": "text",
            "description": "State whose laws govern the Settlement Agreement",
            "current_value": ["California"],
            "options": null,
            "page_no": null,
            "user_value": "New York"
        },
        {
            "category": "Party Information - Plaintiff",
            "label": "Plaintiff Name",
            "type": "text",
            "description": "Name of the Plaintiff entity",
            "current_value": ["ABC Software Corporation"],
            "options": null,
            "page_no": null,
            "user_value": "XYZ Software Corporation"
        }
    ]
}'
import requests
import json

url = "https://api.k-v.ai/api/agent/draft/create_draft"

payload = json.dumps({
    "template_doc_process_id": "264dfa262b748d15ccbeaada89430c68",
    "model_data": {
        "model_name": "gpt-5.1",
        "api_key": ""
    },
    "filled_template_variables": [
        {
            "category": "Agreement Basic Information",
            "label": "Governing Law State",
            "type": "text",
            "description": "State whose laws govern the Settlement Agreement",
            "current_value": ["California"],
            "options": None,
            "page_no": None,
            "user_value": "New York"
        },
        {
            "category": "Party Information - Plaintiff",
            "label": "Plaintiff Name",
            "type": "text",
            "description": "Name of the Plaintiff entity",
            "current_value": ["ABC Software Corporation"],
            "options": None,
            "page_no": None,
            "user_value": "XYZ Software Corporation"
        }
    ]
})

headers = {
    'access-key': 'YOUR_ACCESS_KEY',
    'Content-Type': 'application/json'
}

response = requests.post(url, headers=headers, data=payload)

print(response.text)
const axios = require('axios');

let data = JSON.stringify({
    "template_doc_process_id": "264dfa262b748d15ccbeaada89430c68",
    "model_data": {
        "model_name": "gpt-5.1",
        "api_key": ""
    },
    "filled_template_variables": [
        {
            "category": "Agreement Basic Information",
            "label": "Governing Law State",
            "type": "text",
            "description": "State whose laws govern the Settlement Agreement",
            "current_value": ["California"],
            "options": null,
            "page_no": null,
            "user_value": "New York"
        },
        {
            "category": "Party Information - Plaintiff",
            "label": "Plaintiff Name",
            "type": "text",
            "description": "Name of the Plaintiff entity",
            "current_value": ["ABC Software Corporation"],
            "options": null,
            "page_no": null,
            "user_value": "XYZ Software Corporation"
        }
    ]
});

let config = {
    method: 'post',
    maxBodyLength: Infinity,
    url: 'https://api.k-v.ai/api/agent/draft/create_draft',
    headers: { 
        'access-key': 'YOUR_ACCESS_KEY',
        'Content-Type': 'application/json'
    },
    data: data
};

axios.request(config)
.then((response) => {
    console.log(JSON.stringify(response.data, null, 2));
})
.catch((error) => {
    console.log(error);
});

Important Notes

  • Document ID Consistency: The template_doc_process_id must exactly match the doc_process_id used in Generate Template API
  • Complete Template Required: Include all fields from the template response, even if you're not modifying them
  • user_value Behavior: Set to null to preserve current_value, or provide any value to override
  • HTML Output Format: Returns complete HTML document with inline CSS styling
  • Field Order: Maintain the same field order as received from Generate Template

Next Steps

After creating a draft:

  • Save HTML File: Write the HTML output to a .html file
  • Convert to PDF: Use HTML-to-PDF libraries (e.g., pdfkit, puppeteer, wkhtmltopdf)
  • Review Document: Open in browser to verify all substitutions
  • Generate Multiple Drafts: Reuse the same template with different values
  • Automate Workflows: Integrate into document generation pipelines

Need Help? Contact support at support@k-v.ai

On this page