Skip to content

Commit f0a30ea

Browse files
committed
fix: allow API key from Authorization header for relabel endpoint
- Extract API key from Bearer token in Authorization header - Fall back to CODEBUFF_API_KEY env var if header not provided - Return 401 instead of 500 when no key available - Allows admins to use their own API key without server config
1 parent ebbbe1b commit f0a30ea

File tree

1 file changed

+18
-5
lines changed
  • web/src/app/api/admin/relabel-for-user

1 file changed

+18
-5
lines changed

web/src/app/api/admin/relabel-for-user/route.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,16 @@ export async function POST(req: NextRequest) {
112112
? requestedLimit
113113
: DEFAULT_RELABEL_LIMIT
114114

115-
const apiKey = env.CODEBUFF_API_KEY
115+
// Get API key from Authorization header (preferred) or fall back to env var
116+
const apiKey = getApiKeyFromRequest(req) ?? env.CODEBUFF_API_KEY
116117
if (!apiKey) {
117118
return NextResponse.json(
118119
{
119-
error: 'CODEBUFF_API_KEY is not configured',
120-
details: 'This endpoint now calls LLMs directly (backend was removed) and requires CODEBUFF_API_KEY to be set.',
121-
hint: 'Add CODEBUFF_API_KEY to your environment variables. See .env.example for reference.',
120+
error: 'API key required',
121+
details: 'Provide an API key via Authorization header (Bearer token) or set CODEBUFF_API_KEY env var.',
122+
hint: 'Visit /usage in the web app to create an API key.',
122123
},
123-
{ status: 500 },
124+
{ status: 401 },
124125
)
125126
}
126127

@@ -535,6 +536,18 @@ function buildPromptContext(apiKey: string) {
535536
}
536537
}
537538

539+
/**
540+
* Extract API key from Authorization header (Bearer token)
541+
*/
542+
function getApiKeyFromRequest(req: NextRequest): string | null {
543+
const authHeader = req.headers.get('Authorization')
544+
if (!authHeader?.startsWith('Bearer ')) {
545+
return null
546+
}
547+
const token = authHeader.slice(7).trim()
548+
return token || null
549+
}
550+
538551
async function ensureBigQuery() {
539552
if (!bigqueryReady) {
540553
bigqueryReady = setupBigQuery({ logger })

0 commit comments

Comments
 (0)