gemini-api environments connect, calls out to Google over HTTPS, picks up each tool call and runs it locally. Nothing connects in.
The gemini-e2b template is that machine, ready to go. One command starts it:
- In the terminal, with the
e2bandgemini-apiCLIs. - In the web workbench the sandbox serves: open the link the terminal prints, paste your key, and chat with the agent.
What the template does for you
- One command to start.
gemini-e2bis a prebuilt sandbox: thegemini-apiCLI is installed, pinned and checksum-verified, next togit,python3andnode. There is no image to build and no repository to clone. - The agent cannot read your keys. Both Gemini keys stay in E2B Secrets. The sandbox only holds the word
placeholder, and E2B swaps in the real key on requests to the Gemini API. Because the agent’s shell runs in the same sandbox as the worker, a key passed in as an environment variable would be readable by the agent. Here there is nothing to read. - A web workbench. A browser UI on port
8000stores your key, connects the worker and lets you chat with the agent, with every tool call and every file it writes shown live. - A private link. The workbench is locked by a per-sandbox access token that only the sandbox terminal prints, so only you can open it.
Before you begin
- The E2B CLI, logged in with
e2b auth login, and an E2B API key. - A Gemini API key.
- For option 1, both keys in your shell, plus
jqand thegemini-apiCLI on your machine:
Option 1: in the terminal
1
Create the sandbox
2
Create the environment and store the keys
Create a self-hosted environment, then put your Gemini API key and the environment key into E2B Secrets:The environment key is returned only once, so it goes from the response straight into the vault and is never printed. The ID is a bare UUID: use it as is, without an
environments/ prefix.3
Tell the sandbox to inject them
Add a network rule to the sandbox. E2B then adds both keys to every request the sandbox sends to the Gemini API:
4
Connect the worker
Start the worker with a placeholder instead of the key:The log should end with
Worker ... polling (maxConcurrent=4).5
Give the agent a task
Linux e2b.local ...: its shell ran inside the sandbox. Check the file it wrote:6
Clean up
Option 2: in the web workbench
1
Create the sandbox and open the link
demo-url in the sandbox terminal to print it again.2
Store your Gemini key in the vault
On the first visit, paste your Gemini API key and an E2B API key. The workbench then does what steps 2 and 3 of option 1 do by hand: it creates the environment, stores both Gemini keys in E2B Secrets and adds the network rule to its own sandbox.The E2B API key is used for those two calls and then dropped. Neither key is written to disk or kept in the sandbox.
3
Connect the worker
The vault check calls Google with a placeholder key and turns green only if E2B injected the real one. Then click Connect to start the worker.
4
Give the agent a task
Pick a suggestion or type a task. The answer streams into the chat, Activity shows every tool call the worker runs, and Files shows what the agent wrote to
/home/user/workspace. Follow-up messages continue the same conversation.Who can open the workbench
A sandbox’s port URL is public, so the workbench is locked by an access token:- The token is generated inside the sandbox, one per sandbox, and is printed only in the sandbox terminal. Opening the terminal takes your E2B login, so only you can get the link.
- The token travels in the part of the link after
#, which the browser never sends to a server. The page keeps it for the tab and removes it from the address bar. - Every API call of the workbench without the right token is rejected with
401. Someone with the bare URL sees the page but cannot use it.
From your own code
The same setup with the SDK: store both keys withSecret.create and create the sandbox with the rule that injects them.
Injection is keyed on the host, so any process in the sandbox can still make authenticated calls to the Gemini API, even though it cannot read the key. Other outbound traffic stays open so the agent can install packages. To restrict it, add
allowOut and denyOut as described in internet access.