> For the complete documentation index, see [llms.txt](https://sub2.gitbook.io/sub2/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sub2.gitbook.io/sub2/sdk/listen-for-incoming-payments.md).

# Listen for incoming payments

In this example, we will use the sub2 SDK to set up a listener for incoming payments to our address. This is useful for anyone who wishes to accept payments with sub2 as you can validate that a payment has successfully gone through and update your backend accordingly. It is assumed that sub2-sdk is installed and set up as done [here](/sub2/sdk.md).

```javascript
const unwatch: WatchEventReturnType = sub2SDK.watchIncomingPayments(recipientAddress, callbackFn);
```

Here recipientAddress is of type `` `0x{string}` `` and callbackFn is of type `(payment: Payment) => any`. The callback function is defiend by you and is called on the newly observed payment to the address. For example, you can make a callback function which checks that the subscription index corresponds to an active subscription in your backend and update access accordingly.&#x20;

The returned value `unwatch` of the viem type `WatchEventReturnType` is a function that can be called to stop listening for events.

The Payment type has the following definition and can be imported directly from sub2-sdk:

```typescript
interface Payment {
  sender: `0x${string}`,
  recipient: `0x${string}`,
  subscriptionIndex: bigint,
  sponsor: `0x${string}`,
  amount: bigint,
  token: `0x${string}`,
  protocolFee: bigint,
  processingFee: bigint,
  processingFeeToken: `0x${string}`,
  terms: bigint,
}
```
