Webhooks
Unlock can send real time updates to endpoints.
Locksmith implements Websub which allows anyone to receive real time updates from the Unlock subgraphs. It is a webhook system which many developers will be familiar with with built-in intent verification.
Currently, locksmith support sending updates on new locks and keys. To subscribe, an application will need to send a post request to the hubs located at /api/hooks/[topic]. The body needs to match the schema specified in the Websub w3c spec.

Topics

Our implementation uses the same addresses for topics and their hub.
post
https://locksmith.unlock-protocol.com/api/hooks/:network/locks
/
New locks created
post
https://locksmith.unlock-protocol.com/api/hooks
/:lockAddress/keys
New key purchases
post
https://locksmith.unlock-protocol.com/api/hooks/:network/keys

Example

Let's send a subscribe request to receive updates on new locks.
  1. 1.
    We need to send a subscribe request to the hub located at /api/hooks/:network/locks where network param should be the ID. For example, to receive updates on new locks created on rinkeby network, the endpoint would be /api/hooks/4/locks
  2. 2.
    Make a subscribe request. Here's an example of it in javascript.
1
// Subscribe request to receive updates on new Locks
2
async function subscribe() {
3
const endpoint = "https://locksmith.unlock-protocol.com/api/hooks/4/locks"
4
5
const formData = new FormData()
6
7
formData.set("hub.topic", "https://locksmith-host/api/hooks/4/locks")
8
formData.set("hub.callback", "https://your-webhook-url/")
9
formData.set("hub.mode", "subscribe")
10
formData.set("hub.secret", "unlock-is-best")
11
12
const result = await fetch(endpoint, {
13
method: "POST",
14
body: formData,
15
headers: {
16
"Content-Type": "application/x-www-form-urlencoded"
17
}
18
})
19
20
if(!result.ok) {
21
// Handle the error
22
}
23
const text = await result.text()
24
return text
25
}
Copied!
Once you make a request here with callback URL specified in the Websub W3C specification schema. You will receive an intent verification request on the callback URL. This is an async request which means even if you received a successful response for the subscription request, you are not fully subscribed until your endpoint has confirmed the intent. You won't receive updates if intent confirmation fails for any reason.
To confirm, your endpoint MUST return an HTTP 200 status code, with the hub.challenge value in the body. This value will be sent as a query string to your endpoint by the hub. This is done to prevent spam.
Check out this example written in typescript for reference on how to handle callback intent verification and receive updates https://github.com/unlock-protocol/websub-discord/blob/main/src/middleware.ts