Skip to content

Commit d258254

Browse files
committed
Add API Key authentication
1 parent 2620b2b commit d258254

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

INSTRUCTIONS.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ In this step, you’ll create your first AI Agent with ADK. At this stage, the a
2121

2222
1. Open the file `mongodb-groceries-agent/agent.py`.
2323

24-
2. You’ll see a few Python imports. You’ll use these later to implement the tools. Add the following code after the imports:
24+
1. You’ll see a few Python imports. You’ll use these later to implement the tools. You'll also see a placeholder for a passkey:
25+
26+
```
27+
PASSKEY = "<ASK YOUR INSTRUCTOR FOR THE PASSKEY>"
28+
```
29+
30+
Ask your instructor for the passkey and replace the placeholder with it. This passkey authenticates you to the Google API for this workshop, so you don’t need to provide your own API key—we’ve created one for you.
31+
32+
1. With the API key in place, you’re ready to create your first agent. Add the following code to the file:
2533
2634
```python
2735
root_agent = Agent(
@@ -41,7 +49,7 @@ In this step, you’ll create your first AI Agent with ADK. At this stage, the a
4149
* **instruction** → A system message that defines how the agent should behave (you’ll fill this in later).
4250
* **tools** → Python functions that the agent can call (currently empty).
4351
44-
3. Run the following command in the terminal to start the ADK development UI:
52+
1. Run the following command in the terminal to start the ADK development UI:
4553
4654
```
4755
adk web
@@ -53,11 +61,11 @@ In this step, you’ll create your first AI Agent with ADK. At this stage, the a
5361
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
5462
```
5563
56-
4. Hold CMD (Mac) or CTRL (Windows/Linux) and click on the link: http://127.0.0.1:8000.
64+
1. Hold CMD (Mac) or CTRL (Windows/Linux) and click on the link: http://127.0.0.1:8000.
5765
5866
This opens the development UI where you can chat with your agent.
5967
60-
5. Test your Agent
68+
1. Test your Agent
6169
6270
Try asking your agent:
6371

mongodb-groceries-agent/agent.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
from google.adk.agents import Agent
55
from google import genai
66
from google.genai import types
7+
from utils import set_env
78

9+
PASSKEY = "<ASK YOUR INSTRUCTOR FOR THE PASSKEY>"
10+
set_env(PASSKEY)
11+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")

mongodb-groceries-agent/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests
2+
import os
3+
4+
KEY_ENDPOINT = "https://adk-workshop-480093582215.europe-west1.run.app/"
5+
6+
def set_env(passkey: str) -> None:
7+
"""
8+
Set environment variables in sandbox
9+
10+
Args:
11+
passkey (str): Passkey to get token
12+
"""
13+
payload = {"passkey": passkey}
14+
response = requests.post(url=KEY_ENDPOINT, json=payload)
15+
status_code = response.status_code
16+
if status_code == 200:
17+
result = response.json()
18+
for key in result:
19+
os.environ[key] = result[key]
20+
elif status_code == 401:
21+
raise Exception(f"{response.json()['error']} Ask your instructor for the passkey.")
22+
else:
23+
raise Exception(f"{response.json()['error']}")

0 commit comments

Comments
 (0)