Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(cli): resolve team ID from env var, avoid config mismatch (#1121)

Tomas Srnka575d5bc288a9
diff --git a/.changeset/fix-team-id-env-override.md b/.changeset/fix-team-id-env-override.md
new file mode 100644
index 000000000..e9107d61d
--- /dev/null
+++ b/.changeset/fix-team-id-env-override.md
@@ -0,0 +1,5 @@
+---
+"@e2b/cli": patch
+---
+
+Fix CLI team ID resolution when E2B_API_KEY is set via environment variable. Add E2B_TEAM_ID env var support. Previously, the CLI always used the team ID from ~/.e2b/config.json, causing "Team ID param mismatch" errors when the API key belonged to a different team.
diff --git a/packages/cli/src/api.ts b/packages/cli/src/api.ts
index 7a6fd7bd6..29b39a50e 100644
--- a/packages/cli/src/api.ts
+++ b/packages/cli/src/api.ts
@@ -6,6 +6,7 @@ import { asBold, asPrimary } from './utils/format'
export let apiKey = process.env.E2B_API_KEY
export let accessToken = process.env.E2B_ACCESS_TOKEN
+export const teamId = process.env.E2B_TEAM_ID
const authErrorBox = (keyName: string) => {
let link
@@ -82,6 +83,28 @@ export function ensureAccessToken() {
}
}
+/**
+ * Resolve team ID with proper precedence:
+ * 1. CLI --team flag
+ * 2. E2B_TEAM_ID env var
+ * 3. Local e2b.toml team_id (if provided)
+ * 4. ~/.e2b/config.json teamId (only if E2B_API_KEY env var is NOT set,
+ * to avoid mismatch between env var API key and config file team ID)
+ */
+export function resolveTeamId(
+ cliTeamId?: string,
+ localConfigTeamId?: string
+): string | undefined {
+ if (cliTeamId) return cliTeamId
+ if (teamId) return teamId
+ if (localConfigTeamId) return localConfigTeamId
+ if (!process.env.E2B_API_KEY) {
+ const config = getUserConfig()
+ return config?.teamId
+ }
+ return undefined
+}
+
const userConfig = getUserConfig()
export const connectionConfig = new e2b.ConnectionConfig({
diff --git a/packages/cli/src/commands/template/build.ts b/packages/cli/src/commands/template/build.ts
index 69130ae3d..89f9f2b11 100644
--- a/packages/cli/src/commands/template/build.ts
+++ b/packages/cli/src/commands/template/build.ts
@@ -5,7 +5,12 @@ import * as commander from 'commander'
import * as e2b from 'e2b'
import * as fs from 'fs'
import * as path from 'path'
-import { client, connectionConfig, ensureAccessToken } from 'src/api'
+import {
+ client,
+ connectionConfig,
+ ensureAccessToken,
+ resolveTeamId,
+} from 'src/api'
import { configName, getConfigPath, loadConfig, saveConfig } from 'src/config'
import {
defaultDockerfileName,
@@ -288,13 +293,9 @@ Migration guide: ${asPrimary('https://e2b.dev/docs/template/migration-v2')}`
readyCmd = opts.readyCmd || config.ready_cmd
cpuCount = opts.cpuCount || config.cpu_count
memoryMB = opts.memoryMb || config.memory_mb
- teamID = opts.team || config.team_id
}
- const userConfig = getUserConfig()
- if (userConfig) {
- teamID = teamID || userConfig.teamId
- }
+ teamID = resolveTeamId(opts.team, config?.team_id)
if (config && templateID && config.template_id !== templateID) {
// error: you can't specify different ID than the one in config
@@ -444,6 +445,7 @@ Migration guide: ${asPrimary('https://e2b.dev/docs/template/migration-v2')}`
cwd: root,
})
} catch (err: any) {
+ const userConfig = getUserConfig()
await buildWithProxy(
userConfig,
connectionConfig,
diff --git a/packages/cli/src/commands/template/delete.ts b/packages/cli/src/commands/template/delete.ts
index 7e2c34e78..0ba4ada65 100644
--- a/packages/cli/src/commands/template/delete.ts
+++ b/packages/cli/src/commands/template/delete.ts
@@ -26,9 +26,8 @@ import { getRoot } from 'src/utils/filesystem'
import { listSandboxTemplates } from './list'
import { getPromptTemplates } from 'src/utils/templatePrompt'
import { confirm } from 'src/utils/confirm'
-import { client } from 'src/api'
+import { client, resolveTeamId } from 'src/api'
import { handleE2BRequestError } from '../../utils/errors'
-import { getUserConfig } from 'src/user'
async function deleteTemplate(templateID: string) {
const res = await client.api.DELETE('/templates/{templateID}', {
@@ -84,10 +83,7 @@ export const deleteCommand = new commander.Command('delete')
template_id: template,
})
} else if (opts.select) {
- const userConfig = getUserConfig()
- if (userConfig) {
- teamId = teamId || userConfig.teamId
- }
+ teamId = resolveTeamId(teamId)
const allTemplates = await listSandboxTemplates({
teamID: teamId,
diff --git a/packages/cli/src/commands/template/list.ts b/packages/cli/src/commands/template/list.ts
index 8de09fb4d..c0a934ae7 100644
--- a/packages/cli/src/commands/template/list.ts
+++ b/packages/cli/src/commands/template/list.ts
@@ -4,10 +4,9 @@ import * as e2b from 'e2b'
import { listAliases } from '../../utils/format'
import { sortTemplatesAliases } from 'src/utils/templateSort'
-import { client, ensureAccessToken } from 'src/api'
+import { client, ensureAccessToken, resolveTeamId } from 'src/api'
import { teamOption } from '../../options'
import { handleE2BRequestError } from '../../utils/errors'
-import { getUserConfig } from '../../user'
export const listCommand = new commander.Command('list')
.description('list sandbox templates')
@@ -17,12 +16,11 @@ export const listCommand = new commander.Command('list')
.action(async (opts: { team: string; format: string }) => {
try {
const format = opts.format || 'pretty'
- const userConfig = getUserConfig()
ensureAccessToken()
process.stdout.write('\n')
const templates = await listSandboxTemplates({
- teamID: opts.team || userConfig?.teamId,
+ teamID: resolveTeamId(opts.team),
})
for (const template of templates) {
diff --git a/packages/cli/src/commands/template/publish.ts b/packages/cli/src/commands/template/publish.ts
index a6649f3a1..cfcbdc8e7 100644
--- a/packages/cli/src/commands/template/publish.ts
+++ b/packages/cli/src/commands/template/publish.ts
@@ -20,9 +20,8 @@ import { getRoot } from 'src/utils/filesystem'
import { listSandboxTemplates } from './list'
import { getPromptTemplates } from 'src/utils/templatePrompt'
import { confirm } from 'src/utils/confirm'
-import { client } from 'src/api'
+import { client, resolveTeamId } from 'src/api'
import { handleE2BRequestError } from '../../utils/errors'
-import { getUserConfig } from 'src/user'
async function publishTemplate(templateID: string, publish: boolean) {
const res = await client.api.PATCH('/v2/templates/{templateID}', {
@@ -69,10 +68,7 @@ async function templateAction(
template_id: template,
})
} else if (opts.select) {
- const userConfig = getUserConfig()
- if (userConfig) {
- teamId = teamId || userConfig.teamId
- }
+ teamId = resolveTeamId(teamId)
const allTemplates = await listSandboxTemplates({
teamID: teamId,