Skip to content
Prev Previous commit
Next Next commit
ip key
  • Loading branch information
icecrasher321 committed Apr 7, 2026
commit 5f45d072d7643f6c2856c34d513da0c7316f95bd
34 changes: 19 additions & 15 deletions apps/sim/app/api/speech/token/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,26 @@ export async function POST(request: NextRequest) {
billingUserId = session.user.id
}

if (billingUserId) {
const rateCheck = await rateLimiter.checkRateLimitDirect(
`stt-token:${billingUserId}`,
STT_TOKEN_RATE_LIMIT
const clientIp =
request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ||
request.headers.get('x-real-ip')?.trim() ||
'unknown'

const rateLimitKey = chatId
? `stt-token:chat:${chatId}:${clientIp}`
: `stt-token:user:${billingUserId}`

const rateCheck = await rateLimiter.checkRateLimitDirect(rateLimitKey, STT_TOKEN_RATE_LIMIT)
if (!rateCheck.allowed) {
return NextResponse.json(
{ error: 'Voice input rate limit exceeded. Please try again later.' },
{
status: 429,
headers: {
'Retry-After': String(Math.ceil((rateCheck.retryAfterMs ?? 60000) / 1000)),
},
}
)
if (!rateCheck.allowed) {
return NextResponse.json(
{ error: 'Voice input rate limit exceeded. Please try again later.' },
{
status: 429,
headers: {
'Retry-After': String(Math.ceil((rateCheck.retryAfterMs ?? 60000) / 1000)),
},
}
)
}
}

const apiKey = env.ELEVENLABS_API_KEY
Expand Down