Skip to content

feat: get subscribers endpoint #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Changes from 2 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
99 changes: 92 additions & 7 deletions docs/web3inbox/backend-integration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Example usage:

<TabItem value="javascript" label="JavaScript">

```javascript
```typescript
const PROJECT_ID = '<PROJECT_ID>'
const NOTIFY_API_SECRET = '<NOTIFY_API_SECRET>'
const response = await fetch(`https://notify.walletconnect.com/${PROJECT_ID}/notify`, {
Expand All @@ -64,8 +64,10 @@ const response = await fetch(`https://notify.walletconnect.com/${PROJECT_ID}/not
body: 'The price of Ethereum has gone up 10%',
url: 'https://app.example.com'
},
accounts: ['eip155:1:0x9AfEaC202C837df470b5A145e0EfD6a574B21029']
})
accounts: [
'eip155:1:0x9AfEaC202C837df470b5A145e0EfD6a574B21029'
]
}),
})
```

Expand Down Expand Up @@ -96,7 +98,88 @@ curl -X POST 'https://notify.walletconnect.com/<PROJECT_ID>/notify' \

## Get subscribers

You can get a list of all of the currently-subscribed accounts by calling the `/subscribers` endpoint:
You can tell if an account is subscribed and get information about subscribers using the `/v1/<project-id>/get-subscribers` endpoint. You can provide up to 100 accounts in the request.

If the account is not a subscriber, it will be `null`. If the account is subscribed, it will include a list of notification types the subscriber has opted-into receiving.

Example usage:

<Tabs queryString={'api-client'}>

<TabItem value="javascript" label="JavaScript">

```typescript
const PROJECT_ID = '<PROJECT_ID>'
const NOTIFY_API_SECRET = '<NOTIFY_API_SECRET>'
const requestBody: RequestBody = {
accounts: [
'eip155:1:0x9AfEaC202C837df470b5A145e0EfD6a574B21029',
'eip155:1:0x0000000000000000000000000000000000000000'
]
}

const response = await fetch(`https://notify.walletconnect.com/v1/${PROJECT_ID}/get-subscribers`, {
method: 'POST',
headers: {
Authorization: `Bearer ${NOTIFY_API_SECRET}`
}
body: JSON.stringify(requestBody)
})

const subscribers: Subscribers = await response.json()

type RequestBody = {
// Max 100 accounts
accounts: string[]
}
type Subscribers = {
[account: string]: Subscriber | null
}
type Subscriber = {
notification_types: string[]
}
```

</TabItem>

<TabItem value="curl" label="cURL">

```bash
curl -X POST 'https://notify.walletconnect.com/v1/<PROJECT_ID>/get-subscribers' \
--header 'Authorization: Bearer <NOTIFY_API_SECRET>' \
--header 'Content-Type: application/json' \
--data '{
"accounts": [
"eip155:1:0x9AfEaC202C837df470b5A145e0EfD6a574B21029",
"eip155:1:0x0000000000000000000000000000000000000000"
]
}'
```

</TabItem>

</Tabs>

Example response:

```jsonc
{
"eip155:1:0x9AfEaC202C837df470b5A145e0EfD6a574B21029": {
"notification_types": ["4d1c97ad-c182-4097-8f2c-8f80c0674df2"]
},
"eip155:1:0x0000000000000000000000000000000000000000": null
}
```

## Get all subscribers

:::caution
This endpoint will download _all_ subscribers of your app, which is an expensive operation and can take several seconds to complete. Because of this, it has a low rate limit.
:::

You can get a list of all of the currently-subscribed accounts by calling the `/<project-id>/subscribers` endpoint.

Example usage:

<Tabs queryString={'api-client'}>

Expand Down Expand Up @@ -132,8 +215,10 @@ To protect our system and subscribers, various limits and rate limits are in-pla

Rate limits are implemented as [token bucket](https://en.wikipedia.org/wiki/Token_bucket) and contain both rate and burst amounts. On average, a rate of requests can be made. However, since real-world applications often make requests in bursts, this fixed rate can be surpassed temporarily up to the burst amount, provided the app subsequently makes requests below the average in order to recover its bursting capability.

- `POST /notify`:
- `POST /<project-id>/notify`:
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixing this; was inaccurate

- Each app can send 2 notifications per hour to an account, with a burst up to 50. Accounts that have been rate limited will be returned in the request response. Exceptions may be made on a per-project basis for special circumstances.
- Each app can call this endpoint 2 times per second with a burst up to 20. Rate limited requests will return a 429 status code.
- `GET /subscribers`
- Each app can call this endpoint 1 time per second with a burst up to 5. Rate limited requests will return a 429 status code.
- `POST /v1/<project-id>/get-subscribers`
- Each app can call this endpoint 100 times per second with a burst up to 100. Rate limited requests will return a 429 status code.
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm taking a stab at some numbers for the limits, 100 accounts, 100/s, and 100 burst seem somewhat sensible

- `GET /<project-id>/subscribers`
- Each app can call this endpoint 1 time every 5 minutes with a burst up to 2. Rate limited requests will return a 429 status code.
Copy link
Member Author

@chris13524 chris13524 Feb 6, 2024

Choose a reason for hiding this comment

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

Note: rate limit for old endpoint been reduced. I am working with developers to switch to new endpoint before enforcing the new rate limit.

Copy link
Member

Choose a reason for hiding this comment

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

Can we be explicit about what qualifies as a burst here? I'm a bit confused (and suspect external devs may be) how a burst is counted.

Assuming 1 per 5 minutes but we'll allow 2 if within some short interval?

Copy link
Member Author

Choose a reason for hiding this comment

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

This question is asked a lot. I linked to the token bucket Wikipedia article above and tried to summarize it, but not doing a good job

Copy link
Member Author

Choose a reason for hiding this comment

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

Made an issue: #1401