main
md 234 lines 7.68 KB
Rendered Raw
1 # Adding External APIs Without Code
2
3 One of Agent Zero's most powerful capabilities is the ability to integrate external APIs on the fly without writing any code. By simply providing API documentation or code snippets, the agent can learn to use new services and remember how to use them in future conversations.
4
5 This tutorial demonstrates how to integrate Google's image generation API from Google AI Studio - but the same process works for any public API.
6
7 ## How It Works
8
9 Agent Zero can:
10
11 1. **Analyze API code** - Understand how to use an API from provided snippets or documentation
12 2. **Execute the integration** - Run the code to accomplish your task
13 3. **Remember the solution** - Store the approach in its memory for future use
14 4. **Manage credentials** - Use secrets stored globally or per-project for authentication
15
16 This means you can add capabilities like image generation, translation services, payment processing, or any other API-based feature simply by showing the agent how it works once.
17
18 ## Example: Image Generation with Google AI Studio
19
20 Let's walk through adding image generation capabilities using Google's Gemini API.
21
22 ### Step 1: Get the API Code
23
24 First, we need the code snippet that shows how to use the API.
25
26 1. Go to [Google AI Studio](https://aistudio.google.com/)
27 2. Use the interface to create or test an image generation prompt
28 3. Click **"Get Code"** in the UI
29 4. Select **"Python"** as the language
30 5. Download the code file (or copy it to clipboard)
31
32 ![Getting code from Google AI Studio](../res/usage/api-int/api-int-1.png)
33
34 > [!TIP]
35 > Most API platforms (OpenAI, Anthropic, Replicate, etc.) provide similar "Get Code" features or have documentation with ready-to-use snippets.
36
37 ### Step 2: Provide the Code to Agent Zero
38
39 Now we'll tell Agent Zero to use this code:
40
41 1. Open a chat with Agent Zero
42 2. Send a message like: **"Use this code to generate an image of the Agent Zero logo"**
43 3. Either:
44 - Attach the downloaded Python file, or
45 - Paste the code snippet into the chat
46
47 Agent Zero will analyze the code and understand:
48 - What dependencies are needed (`google-genai` package)
49 - How to structure the API request
50 - What parameters are required
51 - How to handle the response
52
53 ![Agent Zero analyzing the API code](../res/usage/api-int/api-int-2image-gen-api2.png)
54
55 ### Step 3: Configure API Credentials
56
57 On first run, Agent Zero will attempt to use the API but discover it needs an API key:
58
59 ![Missing API key error](../res/usage/api-int/api-int-3-api-key-missing-secrets.png)
60
61 Agent Zero will tell you:
62 - What credential is missing (e.g., `GEMINI_API_KEY`)
63 - Where to configure it (Settings → External Services or Project settings)
64
65 #### Adding the API Key
66
67 You have two options for storing credentials:
68
69 **Option 1: Global Secrets** (available to all chats and projects)
70 1. Click the **Settings** icon in the sidebar
71 2. Go to **External Services**
72 3. Add a new line: `GEMINI_API_KEY=your_actual_key_here`
73 4. Click **Save**
74
75 **Option 2: Project Secrets** (available only within a specific project)
76 1. Open your project settings
77 2. Go to the **Secrets** tab
78 3. Add the key-value pair
79 4. Save the project
80
81 ![Configuring the API key in settings](../res/usage/api-int/api-int-4-secrets-setting.png)
82
83 > [!NOTE]
84 > Global secrets are ideal for APIs you use frequently across different projects. Project secrets are better for client-specific or project-specific integrations.
85
86 ### Step 4: Generate the Image
87
88 After configuring the API key, tell Agent Zero to proceed:
89
90 **"I set the API key in secrets. Now you can use it."**
91
92 Agent Zero will:
93 1. Retrieve the API key from secrets
94 2. Install required dependencies (`google-genai` package)
95 3. Execute the image generation code
96 4. Save the generated image to disk
97 5. Report the file location
98
99 ![Successful image generation](../res/usage/api-int/api-int-5-finish.png)
100
101 The agent confirms:
102 - Mission complete
103 - File location: `/root/agent_zero_logo_0.jpg`
104 - File size: 378 KB
105
106 ## How Memory Helps
107
108 Agent Zero can save useful API patterns in memory so you do not have to explain
109 the same integration every time.
110
111 For example, it may remember:
112
113 - what the API is useful for;
114 - what credential name is needed;
115 - which package or example worked;
116 - how you prefer to use that service in this project.
117
118 > [!IMPORTANT]
119 > Memory still needs curation. If Agent Zero keeps using an old API pattern,
120 > wrong credential name, or outdated package, open Memory and fix or remove that
121 > memory. Memory should help the agent think, not trap it in yesterday's answer.
122
123 See the [Memory Guide](memory.md) for cleanup and curation tips.
124
125 ## Use Cases
126
127 This approach works for any external API. Common examples:
128
129 ### Communication & Notifications
130 - **SendGrid**: Email delivery
131 - **Twilio**: SMS and phone calls
132 - **Slack/Discord**: Message webhooks
133 - **Telegram**: Bot interactions
134
135 ### Data & Analytics
136 - **Google Sheets API**: Spreadsheet automation
137 - **Airtable**: Database operations
138 - **Stripe**: Payment processing
139 - **Plaid**: Banking data
140
141 ### Content & Media
142 - **Unsplash/Pexels**: Stock photos
143 - **ElevenLabs**: Text-to-speech
144 - **Whisper API**: Speech-to-text
145 - **Stable Diffusion**: Image generation
146 - **Replicate**: Various AI models
147
148 ### Development Tools
149 - **GitHub API**: Repository management
150 - **Jira/Linear**: Issue tracking
151 - **Vercel/Netlify**: Deployment
152 - **Docker Hub**: Container registry
153
154 ### Specialized Services
155 - **WeatherAPI**: Weather data
156 - **Google Maps**: Geocoding, directions
157 - **Currency exchange**: Forex rates
158 - **Translation APIs**: Multi-language support
159
160 ## Best Practices
161
162 ### 1. Start with Official Examples
163
164 Always use code snippets from official documentation or API providers' "Get Code" features. These are:
165 - Tested and working
166 - Up-to-date with latest API versions
167 - Include proper error handling
168 - Show recommended practices
169
170 ### 2. Organize Credentials
171
172 **For personal/global APIs:**
173 - Store in **Settings → External Services**
174 - Use clear naming: `SERVICE_API_KEY`, `SERVICE_SECRET`
175 - Add comments to document what each key is for
176
177 **For project-specific APIs:**
178 - Store in **Project Settings → Secrets**
179 - Keeps client data isolated
180 - Prevents accidental cross-project usage
181
182 ### 3. Document in Project Instructions
183
184 When integrating APIs for a specific project, add notes to the project instructions:
185
186 ```markdown
187 ## Available APIs
188
189 This project has access to:
190
191 - **Gemini Image Generation**: Use for creating visuals and illustrations
192 - Credentials: GEMINI_API_KEY (configured in project secrets)
193 - Best for: Professional graphics, concept art, UI mockups
194
195 - **SendGrid Email**: Use for sending automated emails
196 - Credentials: SENDGRID_API_KEY
197 - Best for: Notifications, reports, customer communications
198 ```
199
200 This helps the agent understand what tools are available for the current project.
201
202 ## Advanced: Custom API Wrappers
203
204 For APIs you use frequently, you can have Agent Zero create reusable wrapper functions:
205
206 **"Create a Python module called `image_gen.py` with a function `generate_image(prompt, style='professional')` that uses the Gemini API. Include error handling and save the image to the current project folder."**
207
208 Agent Zero will:
209 1. Create a clean, reusable module
210 2. Add proper documentation
211 3. Include error handling
212 4. Make it easy to call from future tasks
213
214 Then in future chats:
215
216 **"Use the image_gen module to create a logo"**, and it just works!
217
218 ## Conclusion
219
220 By showing Agent Zero a working API example, you can:
221
222 - add a new service to a project;
223 - keep credentials in settings or project secrets;
224 - reuse working patterns later;
225 - clean up memory when an old pattern stops helping.
226
227 This is not magic permanence. It works best when you keep the example, secrets,
228 project instructions, and memories tidy.
229
230 ## Related
231
232 - [Memory Guide](memory.md)
233 - [Projects Guide](projects.md)
234 - [MCP Setup](mcp-setup.md)