Skip to main content

Command Palette

Search for a command to run...

AI Text Generation API Documentation

Published
•3 min read•View as Markdown

1. Overview

This document outlines the usage and specifications of the AI Text Generation API. Powered by advanced artificial intelligence models, this API can generate high-quality text based on user-provided prompts. It is ideal for use cases such as intelligent writing assistants, content creation tools, and chatbots.

2. Basic Information

  • API Name: AI Text Generation API

  • Version: 1.0

  • HTTP Method: POST

  • Endpoint: https://api.example.com/ai/text/generate

3. Request Parameters

ParameterTypeRequiredDescription
promptstringYesInput prompt to guide the text generation
max_tokensintNoMaximum length of generated text, default is 100
temperaturefloatNoControls randomness of output; range: 0–1, default is 0.7

4. Response Parameters

ParameterTypeDescription
codeintResponse status code (200 = success)
messagestringResponse message, empty on success
dataobjectContains the generated text
- textstringThe generated text content

5. Request Example

{
    "prompt": "Write an introduction about artificial intelligence.",
    "max_tokens": 200,
    "temperature": 0.8
}

6. Response Example

Success Response

{
    "code": 200,
    "message": "",
    "data": {
        "text": "Artificial intelligence is a technology that simulates human intelligence, enabling computers to perform tasks such as learning, reasoning, and self-correction."
    }
}

Error Response

{
    "code": 400,
    "message": "Invalid request parameters",
    "data": null
}

7. Error Handling

Error CodeMessageCauseSolution
400Bad RequestInvalid request parametersVerify and correct parameters before resubmitting
401UnauthorizedMissing or invalid credentialsEnsure valid API key is used in the Authorization header
500Internal Server ErrorServer-side issueContact support or retry after some time

8. Security and Authentication

  • Security: Data transmission is secured via HTTPS.

  • Authentication: API key required. Add the Authorization header in the format Bearer your_api_key.

9. Example Code

Python Example

import requests

url = "https://api.example.com/ai/text/generate"
headers = {
    "Authorization": "Bearer your_api_key",
    "Content-Type": "application/json"
}
data = {
    "prompt": "Write an introduction about artificial intelligence.",
    "max_tokens": 200,
    "temperature": 0.8
}

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

if response.status_code == 200:
    print(response.json()["data"]["text"])
else:
    print("Error:", response.status_code, response.json()["message"])

Java Example

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class ApiExample {
    public static void main(String[] args) throws IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.example.com/ai/text/generate"))
            .header("Authorization", "Bearer your_api_key")
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString("{\"prompt\": \"Write an introduction about artificial intelligence.\", \"max_tokens\": 200, \"temperature\": 0.8}"))
            .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() == 200) {
            System.out.println(response.body());
        } else {
            System.out.println("Error: " + response.statusCode() + ", " + response.body());
        }
    }
}

10. Appendix

  • Performance Metrics: Average response time per request is approximately 1 second.

  • Release Notes: Version 1.0 released on June 19, 2025.

  • Reference: OpenAI Official Documentation

Note: This is a sample API reference. Please adjust the details to match the actual specifications of your API when implementing it in a production environment.