Document Your API for AI Agents with OpenAPI
Executive Summary: An OpenAPI specification defines your API's capabilities in a machine-readable format. AI agents (ChatGPT, Claude, Perplexity, etc.) parse this document to understand which operations they can perform, what parameters to send, and what responses to expect. Well-crafted operationIds and descriptions enable agents to select the correct endpoint and fulfill user intent accurately.
Why OpenAPI Documentation Is Critical
When a user asks ChatGPT "Get me the weather forecast for Istanbul tomorrow," the agent must know which API to call, what parameters to send, and how to interpret the response. An OpenAPI (formerly Swagger) specification answers these questions:
- Endpoints and HTTP methods:
GET /weather,POST /orders, etc. - Parameters: Which fields are required, which are optional, and their data types.
- Response schemas: What fields are returned on success, and what error codes mean.
- Security: Authentication requirements such as API keys, OAuth, or bearer tokens.
Without OpenAPI, agents cannot use your API—or worse, they send malformed requests with incorrect parameters. Platforms like ChatGPT Actions, Claude MCP servers, and Perplexity rely on OpenAPI specifications to integrate your service.
How to Write Effective operationIds and Descriptions
operationId
The operationId is a unique identifier for each endpoint. Agents use this ID to reference operations. An effective operationId is:
- Descriptive: Use verb + noun format like
getWeatherForecast,createOrder,deleteUser. - Unique: Must not be repeated anywhere in the document.
- Consistent: Use camelCase or snake_case throughout.
Poor examples: operation1, api2. Good examples: searchProducts, getUserProfile.
Descriptions (description and summary)
The summary provides a brief title, while description offers detailed context. Agents read these to determine when to call each endpoint.
- summary: "Returns 7-day weather forecast for a specified city"
- description: "When the user provides a city name or coordinates, returns daily forecasts including temperature, humidity, wind speed, and precipitation probability. Requires API key authentication."
Add descriptions to parameters as well:
"parameters": [
{
"name": "city",
"in": "query",
"required": true,
"schema": { "type": "string" },
"description": "City name for weather forecast (e.g., Istanbul, London)"
}
]
Agents use these descriptions to map user input to the correct parameters.
Sample openapi.json
Below is an OpenAPI 3.0 specification example for a simple weather API:
{
"openapi": "3.0.0",
"info": {
"title": "Weather Forecast API",
"version": "1.0.0",
"description": "Provides city-based weather forecasts"
},
"servers": [
{
"url": "https://api.example.com/v1"
}
],
"paths": {
"/weather": {
"get": {
"operationId": "getWeatherForecast",
"summary": "7-day weather forecast",
"description": "Returns daily temperature, humidity, and precipitation data for the specified city.",
"parameters": [
{
"name": "city",
"in": "query",
"required": true,
"schema": { "type": "string" },
"description": "City name (e.g., Istanbul)"
},
{
"name": "units",
"in": "query",
"required": false,
"schema": {
"type": "string",
"enum": ["metric", "imperial"],
"default": "metric"
},
"description": "Temperature units (metric=Celsius, imperial=Fahrenheit)"
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"city": { "type": "string" },
"forecast": {
"type": "array",
"items": {
"type": "object",
"properties": {
"date": { "type": "string", "format": "date" },
"temperature": { "type": "number" },
"humidity": { "type": "integer" },
"precipitation": { "type": "number" }
}
}
}
}
}
}
}
},
"400": {
"description": "Invalid city name"
},
"401": {
"description": "Missing or invalid API key"
}
},
"security": [
{
"ApiKeyAuth": []
}
]
}
}
},
"components": {
"securitySchemes": {
"ApiKeyAuth": {
"type": "apiKey",
"in": "header",
"name": "X-API-Key"
}
}
}
}
This specification demonstrates:
- Clear, descriptive
operationId. - Parameters with requirement flags, types, and descriptions.
- Response schema showing how agents should interpret returned data.
- Security scheme defining API key requirements.
Where to Publish Your OpenAPI Specification
1. Host at a Standard URL
Publish your OpenAPI specification at a standard, predictable URL:
https://api.example.com/openapi.jsonhttps://example.com/.well-known/openapi.jsonhttps://example.com/api-docs/openapi.json
The .well-known directory is the recommended location for standardized discovery (RFC 8615). Some tools automatically scan this location.
2. Use in Platform Integrations
ChatGPT Actions: When creating a GPT, paste your OpenAPI specification or provide its URL in the "Actions" section. ChatGPT parses this document to convert user requests into API calls.
Claude MCP: Anthropic's Model Context Protocol servers define tools using OpenAPI-like schemas. In your MCP server's tools/list response, provide each tool's inputSchema in JSON Schema format. (MCP doesn't use OpenAPI directly, but follows similar structure.)
Perplexity and other search agents: Verify with official documentation; some platforms may require manual registration of your OpenAPI URL.
3. robots.txt and Discoverability
If you want your OpenAPI specification to be crawlable, allow it in robots.txt:
User-agent: *
Allow: /openapi.json
Allow: /.well-known/
However, OpenAPI specifications are typically public documents. If you have sensitive endpoints, exclude them from the specification or publish a separate "public" OpenAPI document.
4. Link from Documentation Pages
Add a direct link to your OpenAPI specification on your developer documentation page. You can provide interactive documentation using tools like Swagger UI or Redoc:
<link rel="alternate" type="application/vnd.oai.openapi+json" href="https://api.example.com/openapi.json" />
This <link> tag enables discovery by browsers and tools.
Frequently Asked Questions
Should I use OpenAPI 3.0 or 3.1?
OpenAPI 3.0.x is the most widely supported version. ChatGPT Actions and most tools support 3.0. OpenAPI 3.1 offers full compatibility with JSON Schema 2020-12 and is more modern, but not all platforms support 3.1 yet. For maximum compatibility, use 3.0.3. If your target platform explicitly supports 3.1 (verify in official documentation), you can adopt 3.1 for its enhanced features.
Should I include all my endpoints in the OpenAPI specification?
No. Include only endpoints you want AI agents to access. Carefully evaluate internal admin APIs, endpoints that access sensitive user data, or operations with side effects (payments, deletions). Consider creating a separate "public-ai" OpenAPI specification that includes only safe, read-only endpoints.
Will it work without operationId?
Technically yes, but it's strongly discouraged. Without operationId, agents must reference endpoints by HTTP method + path combination, which is complex and error-prone. Always provide a unique, descriptive operationId for each endpoint—this ensures agents call operations correctly and simplifies debugging.
What should I consider when updating my OpenAPI specification?
Maintain backward compatibility. Don't remove existing parameters or make optional parameters required—this breaks integrated agents. Adding new endpoints or optional parameters is generally safe. Update the version field (e.g., 1.0.0 → 1.1.0) and publish change notes in your documentation. For breaking changes, consider creating a new API version (/v2) instead.