{"record":{"id":"2f40e181ad3207c4","repo":"Budibase/budibase","slug":"workspaceid-is-required","errorCode":null,"errorMessage":"workspaceId is required","messagePattern":"workspaceId is required","errorType":"http","errorClass":"HTTPError","httpStatus":400,"severity":"error","filePath":"packages/server/src/api/controllers/ai/agents.ts","lineNumber":216,"sourceCode":"\n  return await context.doInWorkspaceContext(prodWorkspaceId, async () => {\n    const prodAgent = await context.getWorkspaceDB().tryGet<Agent>(agentId)\n    if (!prodAgent) {\n      return undefined\n    }\n\n    return await configureSlackAppCreationDeployment(agentId)\n  })\n}\n\nconst publishSlackIntegrationForLiveAgent = async (agent: Agent) => {\n  if (!agent.live || !agent._id || !agent.slackIntegration) {\n    return\n  }\n\n  const workspaceId = context.getWorkspaceId()\n  if (!workspaceId) {\n    throw new HTTPError(\"workspaceId is required\", 400)\n  }\n\n  const prodWorkspaceId = db.getProdWorkspaceID(workspaceId)\n  const isPublished = await sdk.workspaces.isWorkspacePublished(prodWorkspaceId)\n  if (!isPublished) {\n    return\n  }\n\n  await context.doInWorkspaceContext(prodWorkspaceId, async () => {\n    const prodAgent = await context.getWorkspaceDB().tryGet<Agent>(agent._id!)\n    if (!prodAgent) {\n      return\n    }\n\n    const { messagingEndpointUrl } = await configureSlackAppCreationDeployment(\n      agent._id!\n    )\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/api/controllers/ai/agents.ts#L198-L234","documentation":"Thrown by publishSlackIntegrationForLiveAgent when a Slack-enabled live agent is being published but no workspace ID is present in the request context. The function needs the workspace to look up the production (published) workspace and check the Slack integration; without context.getWorkspaceId() it cannot proceed and throws this 400 error.","triggerScenarios":"Calling completeSlackOAuth (or the Slack publish flow) from a context lacking a workspace — e.g. a request missing the workspace/appId headers or cookies that identify the tenant, or a background/global operation executed outside a workspace-scoped context.","commonSituations":"Scripts or integrations calling the endpoint without authentication/tenant headers; tests invoking the controller directly without setting up context; session expiry stripping workspace identity; calling the OAuth completion route outside the normal builder/app request flow.","solutions":["Ensure the request carries proper workspace identification (appId/workspace headers or auth context)","Complete the Slack OAuth through the standard UI flow rather than a bare request","Check that authentication middleware ran and populated the tenancy context before this handler","In tests/scripts, set up the workspace context (e.g. context.withWorkspace / doInWorkspace) before invoking"],"exampleFix":"// before\nawait fetch(\"/api/ai/agents/slack/oauth/complete\") // no workspace context\n// after\nawait fetch(\"/api/ai/agents/slack/oauth/complete\", {\n  headers: { \"x-budibase-app-id\": appId },\n  credentials: \"include\"\n})","handlingStrategy":"try-catch","validationCode":"import { context } from \"@budibase/backend-core\"\nfunction assertWorkspaceContext() {\n  const workspaceId = context.getWorkspaceId()\n  if (!workspaceId) {\n    throw new Error(\"workspaceId is required: run this inside a workspace-scoped context\")\n  }\n  return workspaceId\n}\nassertWorkspaceContext()","typeGuard":"function hasWorkspaceContext(ctx) {\n  return typeof ctx?.getWorkspaceId === \"function\" && !!ctx.getWorkspaceId()\n}","tryCatchPattern":"try {\n  await completeSlackOAuth(ctx)\n} catch (err) {\n  if (err.status === 400 && err.message === \"workspaceId is required\") {\n    throw new Error(\"Slack OAuth completion requires an authenticated, workspace-scoped request\")\n  }\n  throw err\n}","preventionTips":["Always complete Slack OAuth from an authenticated app/builder request with workspace headers","In tests and scripts, wrap calls in context/doInWorkspace with a real workspace ID","Verify auth middleware runs before workspace-dependent handlers","Check context.getWorkspaceId() early in custom flows that fan out to agent publishing"],"tags":["tenancy","context","http-400","slack-integration"],"backgroundTag":"missing-workspace-context","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}