Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/docs/libraries/ts/ratelimit/override/delete-override.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: "Delete Override"
description: "Deletes an override"
---

## Request
<ParamField body="identifier" type="string" required>
Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

<Warning>
Either `namespaceId` or `namespaceName` is required. Not both.
</Warning>

<ParamField body="namespaceId" type="string">
The id of the namespace. Either namespaceId or namespaceName must be provided
</ParamField>

<ParamField body="namespaceName" type="string">
Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>


## Response

No response, but if no error is returned the override has been deleted successfully.


<RequestExample>
```ts
await unkey.deleteOverride({
identifier: "user_123",
namespaceName: "email.outbound",
})

```
```ts
await unkey.deleteOverride({
identifier: "user_123",
namespaceId:"rlns_12345",
})
```
</RequestExample>

80 changes: 80 additions & 0 deletions apps/docs/libraries/ts/ratelimit/override/get-override.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: "Get Override"
description: "Gets a ratelimit override"
---

## Request
<ParamField body="identifier" type="string" required>
Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

<Warning>
Either `namespaceId` or `namespaceName` is required. Not both.
</Warning>

<ParamField body="namespaceId" type="string">
The id of the namespace. Either namespaceId or namespaceName must be provided
</ParamField>

<ParamField body="namespaceName" type="string">
Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>


## Response

<ResponseField name="result">
<Expandable title="properties" defaultOpen>

<ParamField body="id" type="string" required>
Identifier of the override requested
</ParamField>

<ParamField body="identifier" type="string" required>
Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

<ParamField body="limit" type="number" required>
How many requests may pass in a given window.
</ParamField>

<ParamField body="duration" type="number" required>
The window duration in milliseconds.
</ParamField>

<ParamField body="async" type="boolean">
Async will return a response immediately, lowering latency at the cost of accuracy.
</ParamField>
</Expandable>
</ResponseField >


<RequestExample>
```ts
const override = await unkey.getOverride({
identifier:"user.example",
namespaceName: "email.outbound"
});

```
```ts
const override = await unkey.getOverride({
identifier:"user.example",
namespaceId: "rlns_1234",
});
```
</RequestExample>

<ResponseExample>
```ts
{
result: {
id: "rlor_4567",
identifier: "user.example",
limit: 10,
duration: 60000,
async: false
}
}
```
</ResponseExample>
96 changes: 96 additions & 0 deletions apps/docs/libraries/ts/ratelimit/override/list-overrides.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
title: "List Overrides"
description: "Lists all overrides"
---

## Request

<Warning>
Either `namespaceId` or `namespaceName` is required. Not both.
</Warning>

<ParamField body="namespaceId" type="string">
The id of the namespace. Either namespaceId or namespaceName must be provided
</ParamField>

<ParamField body="namespaceName" type="string">
Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

## Response

<ResponseField name="result">
<Expandable title="properties" defaultOpen>

<ResponseField name="overrides" type="Array" required>

<Expandable>
<ParamField body="id" type="string" required>
Identifier of the override requested
</ParamField>

<ParamField body="identifier" type="string" required>
Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

<ParamField body="limit" type="number" required>
How many requests may pass in a given window.
</ParamField>

<ParamField body="duration" type="number" required>
The window duration in milliseconds.
</ParamField>

<ParamField body="async" type="boolean" optional>
Async will return a response immediately, lowering latency at the cost of accuracy.
</ParamField>

</Expandable>
</ResponseField>

<ResponseField name="total" type="number" required>
The total number of overrides
</ResponseField>
<ResponseField name="cursor" type="string" optional>
The cursor to use for pagination
</ResponseField>
</Expandable>

</ResponseField>


<RequestExample>
```ts
const overrides = await unkey.listOverrides({
namespaceName: "email.outbound"
});
```

```ts
const overrides = await unkey.listOverrides({
nameSpaceId:"rlns_12345",
});
```
</RequestExample>


<ResponseExample>
```ts
{
result: {
overrides: [
{
id: 'rlor_1234',
identifier: 'customer_123',
limit: 10,
duration: 50000,
async: false
}
],
total: 1,
cursor: 'rlor_1234'
}
}
```

</ResponseExample>
48 changes: 48 additions & 0 deletions apps/docs/libraries/ts/ratelimit/override/overview.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "Overview"
description: "Ratelimit overrides"
---


Ratelimit overrides are a way to override the ratelimit for specific users or group using an identifier.

## Configure your override

```ts
import { Override } from "@unkey/ratelimit"

const unkey = new Override({
rootKey: process.env.UNKEY_ROOT_KEY,
})
```

## Use it

```ts
async function handler(request) {

const identifier = request.getUserId() // or ip or anything else you want

const override = await unkey.setOverride({
identifier: identifier,
limit: 10,
duration: 60000,
namespaceName: "email.outbound",
async: true
})
if (override.error){
// handle the error here
console.error(override.error.message);
return;
}
// handle the request here
}
```

There are four main functions to interact with overrides:

- [setOverride](/libraries/ts/ratelimit/override/set-override) Sets an override for a ratelimit.
- [getOverride](/libraries/ts/ratelimit/override/get-override) Gets a ratelimit override.
- [deleteOverride](/libraries/ts/ratelimit/override/delete-override) Deletes an override.
- [listOverrides](/libraries/ts/ratelimit/override/list-overrides) Lists all overrides for a namnespace.

77 changes: 77 additions & 0 deletions apps/docs/libraries/ts/ratelimit/override/set-override.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: "Set Override"
description: "Sets an override for a ratelimit"
---

## Request


<ParamField body="identifier" type="string" required>
Identifier of your user, this can be their userId, an email, an ip or anything else. Wildcards ( * ) can be used to match multiple identifiers, More info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

<ParamField body="limit" type="number" required>
How many requests may pass in a given window.
</ParamField>

<ParamField body="duration" type="number" required>
The window duration in milliseconds.
</ParamField>

<Warning>
Either `namespaceId` or `namespaceName` is required. Not both.
</Warning>

<ParamField body="namespaceId" type="string">
The id of the namespace. Either namespaceId or namespaceName must be provided
</ParamField>

<ParamField body="namespaceName" type="string">
Namespaces group different limits together for better analytics. You might have a namespace for your public API and one for internal tRPC routes. Wildcards can also be used, more info can be found at https://www.unkey.com/docs/ratelimiting/overrides#wildcard-rules
</ParamField>

<ParamField body="async" type="boolean" default="false">
Async will return a response immediately, lowering latency at the cost of accuracy.
</ParamField>


## Response

<ResponseField name="result">
<Expandable title="properties" defaultOpen>
<ResponseField name="overrideId" type="string" required>
The id of the override that was set.
</ResponseField>
</Expandable>
</ResponseField>
<RequestExample>
```ts
const override = await unkey.setOverride({
identifier: "user_123",
limit: 10,
duration: 60000,
namespaceName: "email.outbound",
async: true
})
```

```ts
const override = await unkey.setOverride({
identifier: "user_123",
limit: 5,
duration: 50000,
namespaceId: "rlns_1234",
async: false
})
```
</RequestExample>

<ResponseExample>
```ts
{
result: {
overrideId: 'rlor_12345'
}
}
```
</ResponseExample>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "@unkey/ratelimit"
title: "Ratelimit"
description: "Serverless ratelimiting"
---

Expand Down
1 change: 0 additions & 1 deletion apps/docs/libraries/ts/sdk/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const unkey = new Unkey({
```
</ParamField>


### Retries

By default the client will retry on network errors, you can customize this behavior:
Expand Down
17 changes: 16 additions & 1 deletion apps/docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,22 @@
}
]
},
"libraries/ts/ratelimit",
{
"group": "@unkey/ratelimit",
"pages": [
"libraries/ts/ratelimit/ratelimit",
{
"group": "Overrides",
"pages": [
"libraries/ts/ratelimit/override/overview",
"libraries/ts/ratelimit/override/set-override",
"libraries/ts/ratelimit/override/get-override",
"libraries/ts/ratelimit/override/list-overrides",
"libraries/ts/ratelimit/override/delete-override"
]
}
]
},
"libraries/ts/nextjs",
"libraries/ts/hono",
"libraries/ts/cache/overview",
Expand Down