Skip to content

Commit aba3d43

Browse files
committed
stt api fix
1 parent f4addc9 commit aba3d43

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

src/routes/api/stt/+server.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,40 @@ const COST_PER_MINUTE: Record<string, number> = {
1313
'Elevenlabs-STT': 0.03,
1414
};
1515

16-
const getExplicitNanoGPTKey = (request: Request): string | null => {
16+
type ResolvedNanoGPTKey = {
17+
value: string;
18+
source: 'authorization' | 'x-api-key' | 'derived';
19+
};
20+
21+
const getExplicitNanoGPTKey = (request: Request): ResolvedNanoGPTKey | null => {
1722
const headerKey = request.headers.get('x-api-key');
1823
if (headerKey && !headerKey.startsWith('nc_')) {
19-
return headerKey;
24+
return { value: headerKey, source: 'x-api-key' };
2025
}
2126

2227
const authHeader = request.headers.get('Authorization');
2328
if (authHeader?.startsWith('Bearer ')) {
2429
const token = authHeader.slice(7).trim();
2530
if (token.length > 0 && !token.startsWith('nc_')) {
26-
return token;
31+
return { value: token, source: 'authorization' };
2732
}
2833
}
2934

3035
return null;
3136
};
3237

33-
const resolveNanoGPTKey = async (request: Request, userId?: string): Promise<string | null> => {
38+
const resolveNanoGPTKey = async (
39+
request: Request,
40+
userId?: string
41+
): Promise<ResolvedNanoGPTKey | null> => {
3442
const explicitKey = getExplicitNanoGPTKey(request);
3543
if (explicitKey) return explicitKey;
3644

3745
if (!userId) return null;
3846

3947
const userKey = await getUserKey(userId, 'nanogpt');
40-
return userKey || env.NANOGPT_API_KEY || null;
48+
const derivedKey = userKey || env.NANOGPT_API_KEY;
49+
return derivedKey ? { value: derivedKey, source: 'derived' } : null;
4150
};
4251

4352
export const POST: RequestHandler = async ({ request, fetch }) => {
@@ -60,20 +69,24 @@ export const POST: RequestHandler = async ({ request, fetch }) => {
6069
}
6170

6271
const userId = await tryGetAuthenticatedUserId(request);
63-
const apiKey = await resolveNanoGPTKey(request, userId);
72+
const resolvedKey = await resolveNanoGPTKey(request, userId);
6473

65-
if (!apiKey) {
74+
if (!resolvedKey) {
6675
return json({ error: 'Authentication required or NanoGPT API key missing' }, { status: 401 });
6776
}
6877

78+
const upstreamHeaders: Record<string, string> = {};
79+
// NanoGPT STT expects x-api-key; only use Authorization when explicitly provided.
80+
if (resolvedKey.source === 'authorization') {
81+
upstreamHeaders.Authorization = `Bearer ${resolvedKey.value}`;
82+
} else {
83+
upstreamHeaders['x-api-key'] = resolvedKey.value;
84+
}
85+
6986
const start = Date.now();
7087
const response = await fetch('https://nano-gpt.com/api/transcribe', {
7188
method: 'POST',
72-
headers: {
73-
'x-api-key': apiKey,
74-
Authorization: `Bearer ${apiKey}`,
75-
// Do NOT set Content-Type, allow fetch to set boundary automatically with FormData
76-
},
89+
headers: upstreamHeaders,
7790
body: formData,
7891
});
7992

0 commit comments

Comments
 (0)