Generations API – POST /api/generate-code-optimization

Generate AI-powered optimized and refactored code suggestions to improve readability, simplicity, and performance.Β 


πŸ” Authentication

All Generations API endpoints require API Token authentication.

Clients must include a valid bearer token in the Authorization header.


πŸš€ Endpoint Overview

{
    "title": "Generate Code Optimization",
    "description": "Refactor and optimize provided source code using AI",
    "method": "POST",
    "baseUrl": "https://app.docuwriter.ai",
    "endpoint": "/api/generate-code-optimization",
    "headers": [
        {
            "key": "Authorization",
            "value": "Bearer <token>",
            "required": true
        },
        {
            "key": "Content-Type",
            "value": "application/json",
            "required": true
        }
    ],
    "queryParams": [],
    "pathParams": [],
    "bodyType": "json",
    "requestBody": "{\n  \"source_code\": \"<?php for($i=0; $i<1000; $i++){ echo $i; } ?>\",\n  \"filename\": \"loop.php\",\n  \"name\": \"LoopRefactor\",\n  \"additional_instructions\": \"Use arrow functions where possible\"\n}",
    "formData": [],
    "rawBody": "",
    "responses": {
        "200": {
            "description": "Success",
            "body": "{\n  \"success\": true,\n  \"data\": {\n    \"generation\": \"**loop.php**\\n```php\\nfor ($i = 0; $i < 1000; $i++) {\\n    echo $i;\\n}\\n```\\n*Optimizations made:* ...\"\n  },\n  \"message\": \"Code optimization generated successfully\"\n}"
        },
        "400": {
            "description": "Payload too large",
            "body": "{\n  \"success\": false,\n  \"message\": \"The provided code is too large for processing. Please reduce the amount of code and try again.\"\n}"
        },
        "403": {
            "description": "Forbidden (credits exhausted or viewer role)",
            "body": "{\n  \"success\": false,\n  \"message\": \"You have no remaining credits. Please upgrade your plan.\"\n}"
        },
        "422": {
            "description": "Validation Error",
            "body": "{\n  \"message\": \"The given data was invalid.\",\n  \"errors\": {\n    \"source_code\": [\"The source_code field is required.\"],\n    \"filename\": [\"The filename field is required.\"]\n  }\n}"
        },
        "500": {
            "description": "Server or AI service error",
            "body": "{\n  \"success\": false,\n  \"message\": \"Error generating optimization. Please try again.\"\n}"
        }
    }
}

πŸ“₯ Request Parameters

Field Type Required Description
source_code string yes Raw source code to refactor and optimize.
filename string yes Original file name (e.g., MyClass.php).
name string no Custom name for the generation record (max 255 characters).
additional_instructions string no Extra guidance for the AI (e.g., β€œUse modern PHP syntax”).

πŸ“€ Response Body

  • success (boolean): Indicates request status.
  • data.generation (string): Markdown-formatted optimized code.
  • message (string): Human-readable status message.