OpenAI
Integrate GPT-4o, DALL-E 3, Whisper, and more with ProtectMyAPI.
🧠
Supported Models: GPT-4o, GPT-4o-mini, GPT-4 Turbo, DALL-E 3, Whisper, TTS
Features
- ✅ Chat Completions (text generation)
- ✅ Vision (image analysis)
- ✅ Image Generation (DALL-E)
- ✅ Audio Transcription (Whisper)
- ✅ Text-to-Speech (TTS)
- ✅ Function Calling / Tools
- ✅ Streaming responses
Setup
1. Add your API key to Secrets
In the ProtectMyAPI dashboard:
- Go to your app → Secrets
- Add a secret named
OPENAI_API_KEY - Paste your OpenAI API key as the value
2. Create an endpoint
Create an endpoint with:
- Name: OpenAI Chat
- Slug:
openai-chat - Target URL:
https://api.openai.com/v1/chat/completions - Method: POST
- Auth Type: Bearer
- Auth Value:
{{OPENAI_API_KEY}}
Code Examples
import ProtectMyAPI
// Simple chat
let response = try await ProtectMyAPI.shared.request(
endpoint: "openai-chat",
method: .POST,
body: [
"model": "gpt-4o",
"messages": [
["role": "system", "content": "You are a helpful assistant."],
["role": "user", "content": "Hello!"]
]
]
)
// Streaming
for try await chunk in ProtectMyAPI.shared.stream(
endpoint: "openai-chat",
body: [
"model": "gpt-4o",
"messages": [["role": "user", "content": "Write a poem"]],
"stream": true
]
) {
print(chunk, terminator: "")
}
// Vision (image analysis)
let visionResponse = try await ProtectMyAPI.shared.request(
endpoint: "openai-chat",
method: .POST,
body: [
"model": "gpt-4o",
"messages": [
[
"role": "user",
"content": [
["type": "text", "text": "What's in this image?"],
["type": "image_url", "image_url": ["url": "data:image/jpeg;base64,..."]]
]
]
]
]
)Models
| Model | Best For | Context |
|---|---|---|
gpt-4o | Best quality, vision | 128K |
gpt-4o-mini | Fast, affordable | 128K |
gpt-4-turbo | Complex tasks | 128K |
gpt-3.5-turbo | Simple tasks | 16K |
Function Calling
let response = try await ProtectMyAPI.shared.request(
endpoint: "openai-chat",
method: .POST,
body: [
"model": "gpt-4o",
"messages": [["role": "user", "content": "What's the weather in Paris?"]],
"tools": [
[
"type": "function",
"function": [
"name": "get_weather",
"description": "Get current weather",
"parameters": [
"type": "object",
"properties": [
"location": ["type": "string"]
]
]
]
]
]
]
)Image Generation (DALL-E)
Create an endpoint for images:
- Slug:
openai-images - Target URL:
https://api.openai.com/v1/images/generations
let imageResponse = try await ProtectMyAPI.shared.request(
endpoint: "openai-images",
method: .POST,
body: [
"model": "dall-e-3",
"prompt": "A beautiful sunset over mountains",
"size": "1024x1024",
"quality": "hd"
]
)