Google Gemini
Integrate Gemini 2.0 Flash, Gemini Pro, and Imagen with ProtectMyAPI.
๐ฌ
Supported Models: Gemini 2.0 Flash, Gemini 1.5 Pro, Gemini 1.5 Flash, Imagen 3
Features
- โ Text generation
- โ Vision (image analysis)
- โ Audio understanding
- โ Video understanding
- โ Image generation (Imagen)
- โ Search Grounding
- โ Function Calling
- โ Streaming responses
Setup
1. Add your API key to Secrets
In the ProtectMyAPI dashboard:
- Go to your app โ Secrets
- Add a secret named
GOOGLE_AI_API_KEY - Paste your Google AI API key as the value
2. Create an endpoint
Create an endpoint with:
- Name: Gemini Generate
- Slug:
gemini-generate - Target URL:
https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={{GOOGLE_AI_API_KEY}} - Method: POST
Code Examples
import ProtectMyAPI
// Simple text generation
let response = try await ProtectMyAPI.shared.request(
endpoint: "gemini-generate",
method: .POST,
body: [
"contents": [
[
"parts": [
["text": "Explain how AI works"]
]
]
]
]
)
// With system instruction
let response = try await ProtectMyAPI.shared.request(
endpoint: "gemini-generate",
method: .POST,
body: [
"system_instruction": [
"parts": [["text": "You are a helpful coding assistant."]]
],
"contents": [
[
"parts": [
["text": "Write a Python function to reverse a string"]
]
]
]
]
)
// Vision (image analysis)
let imageData = UIImage(named: "photo")!.jpegData(compressionQuality: 0.8)!
let base64Image = imageData.base64EncodedString()
let visionResponse = try await ProtectMyAPI.shared.request(
endpoint: "gemini-generate",
method: .POST,
body: [
"contents": [
[
"parts": [
["text": "What's in this image?"],
[
"inline_data": [
"mime_type": "image/jpeg",
"data": base64Image
]
]
]
]
]
]
)Models
| Model | Best For | Context |
|---|---|---|
gemini-2.0-flash | Fast, multimodal | 1M |
gemini-1.5-pro | Complex tasks | 2M |
gemini-1.5-flash | Fast responses | 1M |
Search Grounding
Ground responses with Google Search:
let response = try await ProtectMyAPI.shared.request(
endpoint: "gemini-generate",
method: .POST,
body: [
"contents": [
[
"parts": [["text": "What are the latest AI news?"]]
]
],
"tools": [
["google_search": [:]]
]
]
)Audio Understanding
let audioData = // your audio as base64
let response = try await ProtectMyAPI.shared.request(
endpoint: "gemini-generate",
method: .POST,
body: [
"contents": [
[
"parts": [
["text": "Transcribe this audio"],
[
"inline_data": [
"mime_type": "audio/mp3",
"data": audioData
]
]
]
]
]
]
)Image Generation (Imagen)
Create an endpoint for Imagen:
- Slug:
imagen-generate - Target URL:
https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-001:predict?key={{GOOGLE_AI_API_KEY}}
let imageResponse = try await ProtectMyAPI.shared.request(
endpoint: "imagen-generate",
method: .POST,
body: [
"instances": [
["prompt": "A futuristic city at sunset"]
],
"parameters": [
"sampleCount": 1
]
]
)