Skip to content

Conversation

@mrmauer
Copy link
Contributor

@mrmauer mrmauer commented Jan 15, 2025

After PR

Introduces ApiKeyValidateRateLimitedException error for rate limiting end-user api keys. This includes the fields waitSeconds (decimal of seconds until a key/user/org is no longer rate limited), userFacingError, errorCode (always rate_limit_exceeded for this case).

Example use once ApiKeyValidateRateLimitedException is passed through the node repo:

// Endpoint that returns user's information
app.get('/check_key', async (req, res) => {
    const authorizationHeader = req.get('Authorization');
    try {
        const inputKey = authorizationHeader.split(' ')[1];
        const validation = await auth.validateApiKey(inputKey);
        res.json(validation);
    } catch (err) {
        if (err instanceof ApiKeyValidateRateLimitedException) {
            res.status(429).send(`{"error": "Rate limited for ${err.waitSeconds} seconds"}`);
        } else if (err instanceof ApiKeyValidateException) {
            res.status(401).send(`{"error": "Invalid key"}`);
        } else {
            res.status(500).send(`{"error": "Internal server error"}`);
        }
    }
});

Tests

Using yalc and publishing to a local repo of the node library, I verified the expected behavior in an example app that validates api keys

Copy link
Contributor

@andrew-propelauth andrew-propelauth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants