Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Update / Enforce API rate limits

Blue Mouse48a4733056e6
diff --git a/backend/apps/cloud/src/auth/guards/api-key-rate-limit.guard.ts b/backend/apps/cloud/src/auth/guards/api-key-rate-limit.guard.ts
index ce42eb2b2..e4a9fde4b 100644
--- a/backend/apps/cloud/src/auth/guards/api-key-rate-limit.guard.ts
+++ b/backend/apps/cloud/src/auth/guards/api-key-rate-limit.guard.ts
@@ -1,6 +1,10 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
import { checkRateLimitForApiKey } from '../../common/utils'
-import { PlanCode } from '../../user/entities/user.entity'
+import {
+ getEffectivePlanType,
+ getPlanTypeEntitlements,
+ PlanCode,
+} from '../../user/entities/user.entity'
@Injectable()
export class ApiKeyRateLimitGuard implements CanActivate {
@@ -11,12 +15,19 @@ export class ApiKeyRateLimitGuard implements CanActivate {
if (request.headers['x-api-key']) {
if (!user) return false
+ const effectivePlanType = getEffectivePlanType(user)
+ const entitlements = effectivePlanType
+ ? getPlanTypeEntitlements(effectivePlanType)
+ : null
+ const planTypeLimit = entitlements?.apiRateLimitPerHour
const reqAmount =
user.planCode === PlanCode.none ||
user.isAccountBillingSuspended ||
user.dashboardBlockReason !== null
? 0
- : user.maxApiKeyRequestsPerHour
+ : typeof planTypeLimit === 'number'
+ ? planTypeLimit
+ : user.maxApiKeyRequestsPerHour
return checkRateLimitForApiKey(user.apiKey, reqAmount)
}
diff --git a/backend/apps/cloud/src/user/entities/user.entity.ts b/backend/apps/cloud/src/user/entities/user.entity.ts
index f115ec065..16145a53a 100644
--- a/backend/apps/cloud/src/user/entities/user.entity.ts
+++ b/backend/apps/cloud/src/user/entities/user.entity.ts
@@ -65,14 +65,14 @@ export const PLAN_TYPE_ENTITLEMENTS = {
websites: 50,
teamMembers: 10,
organisations: 3,
- apiRateLimitPerHour: 600,
+ apiRateLimitPerHour: 300,
sessionReplaysIncluded: 0,
},
[PlanType.plus]: {
websites: 100,
teamMembers: 25,
organisations: 10,
- apiRateLimitPerHour: 5000,
+ apiRateLimitPerHour: 6000,
sessionReplaysIncluded: 50000,
},
[PlanType.enterprise]: {
diff --git a/docs/content/docs/api/stats.mdx b/docs/content/docs/api/stats.mdx
index c8d539ce4..4bd0d8bef 100644
--- a/docs/content/docs/api/stats.mdx
+++ b/docs/content/docs/api/stats.mdx
@@ -9,11 +9,12 @@ The statistics API provides several GET endpoints. These endpoints require some
Each request must be authenticated with an API key using `X-Api-Key` HTTP header. You can obtain an API key in your Swetrix [account settings](https://swetrix.com/user-settings).
-Rate limit for the API depends on your plan, you can find more information on the billing (or the main) page.
-As of 9 February 2023, the rate limits are as follows:
+Rate limit for the API depends on your plan. You can find more information on the billing page.
+The current rate limits are:
-- **Free plan**: 600 requests per hour;
-- **Any paid plan**: 600 requests per hour.
+- **Standard plan**: 300 requests per hour;
+- **Plus plan**: 6000 requests per hour;
+- **Enterprise plan**: custom, and may be unlimited on demand.
If you have special needs for more requests, please contact us to request more capacity.