1. Create a Google Account: If you don't have one, sign up for a free Google Account, which is required to access Google AI Studio and manage API keys.
2. Access Google AI Studio: Navigate to aistudio.google.com and log in with your Google Account. This web-based environment is your primary hub for exploring Gemini models and managing API access.
3. Generate Your API Key: Within Google AI Studio, locate the 'Get API key' or 'API key' section. Click 'Create API key in new project' or 'Create API key' to generate a unique credential. Copy this key immediately.
4. Install Gemini API SDK (Optional but Recommended): For Python, install with pip install google-generativeai. For Node.js, install with npm install @google/generativeai. These SDKs simplify API interactions.
5. Make Your First Text Generation Request (Python Example): Set your API key as an environment variable (export API_KEY='YOUR_API_KEY'). Then, use the following Python code:
python
import google.generativeai as genai
import os
genai.configure(api_key=os.environ['API_KEY'])
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content('Write a short story about a brave knight.')
print(response.text)
6. Make Your First Text Generation Request (Node.js Example): Set your API key as an environment variable (export API_KEY='YOUR_API_KEY'). Then, use the following Node.js code:
javascript
const { GoogleGenerativeAI } = require('@google/generative-ai');
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
async function run() {
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
const prompt = 'Write a short story about a brave knight.';
const result = await model.generateContent(prompt);
const response = await result.response;
console.log(response.text());
}
run();
7. Make Your First Text Generation Request (cURL Example): Replace YOUR_API_KEY with your actual key.
bash
curl -X POST 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{ "contents": [ { "parts": [ { "text": "Write a short story about a brave knight." } ] } ] }'
8. Understand Basic Authentication: The Gemini API primarily uses API keys for authentication. These keys are passed either as a query parameter (?key=YOUR_API_KEY) or via the Authorization header for more secure contexts. Always protect your API keys.
9. Monitor API Usage and Billing: In Google AI Studio or the Google Cloud Console, review your API usage dashboard. This shows token consumption for different models and helps track costs, especially when transitioning from the free tier.