Add AI Features
Build a chat completion endpoint and add AI-powered features with the OpenAI addon.
Add AI Features
Build AI-powered endpoints — chat, summarization, content moderation — using the OpenAI addon.
What you will build
- POST /api/ai/chat — send a message and get an AI response - POST /api/ai/summarize — summarize a block of text - A content moderation pattern for user submissions
Steps
Install the OpenAI addon
Go to Addons and install OpenAI. Add your API key as OPENAI_API_KEY in Settings > Environment Variables.
Build a chat endpoint
Create POST /api/ai/chat:
1. External API Call — Method: POST, URL: 'https://api.openai.com/v1/responses', Auth: Bearer env.OPENAI_API_KEY, Body:
{
"model": "<current-openai-model>",
"instructions": "You are a helpful assistant.",
"input": "input.message"
}
Assign To: aiResponse
2. Set Variable — Name: reply, Value: vars.aiResponse.body.output_text
3. Respond — Status: 200, Body: { reply: vars.reply }
Send { "message": "What is Spala?" } to test it.
Build a summarization endpoint
Create POST /api/ai/summarize — same pattern, different system prompt:
1. External API Call — same setup, but with a different instruction:
{
"model": "<current-openai-model>",
"instructions": "Summarize the following text in 2-3 sentences.",
"input": "input.text"
}
Assign To: result
2. Respond — Body: { summary: vars.result.body.output_text }
Add content moderation
Before saving user-generated content, check it with OpenAI's moderation endpoint:
1. External API Call — URL: 'https://api.openai.com/v1/moderations', Body: { input: input.content }, Assign To: moderation
2. If/Else — Condition: vars.moderation.body.results | first | get('flagged') == true
- True: Respond — Status: 400, Body: { error: 'Content violates community guidelines' }
- False: proceed with saving the content
Handle errors gracefully
Wrap API calls in a Try/Catch step. In the Catch branch, check the status code — 429 means rate-limited (tell the user to retry), and 500+ means a provider error (return a generic error message).
Pro tip
The same pattern works with other AI providers. Swap the URL and authentication to use Anthropic, Mistral, or any OpenAI-compatible API.
Addons /addons Browse AI and other integrations Build a REST API /guides/build-rest-api Create the API structure for your AI features