In this tutorial we'll see how to use the Notification REST API to create a LUSID webhook notification.
Let's imagine we want to automatically seed all new portfolios we create with some cash. To do this, we need to subscribe to the portfolio created event that LUSID emits every time a transaction portfolio is created and attach a webhook notification to post to the LUSID UpsertTransactions API.
Note: To complete this tutorial, you must have suitable access control permissions. This can most easily be achieved by assigning your LUSID user the built-in
lusid-administrator
role, which should already be the case if you are the domain owner. If you are informed you do not have a license to perform a certain operation, contact support.
Step 1: Subscribing to the portfolio created event
Before we can create a notification, we need to subscribe to the portfolio created event using the CreateSubscription API, passing in:
An API access token as a Bearer token in the Authorization HTTP header. See how to obtain a token.
PortfolioEvent
as ourscope
andAnyPortfolioCreated
as ourcode
for the subscription.A
displayName
anddescription
for the subscription.The
eventType
to subscribe to. In our case, we want to subscribe to thePortfolioCreated
event type every time it occurs, so we leave thefilter
parameter empty.
curl -X POST "https://<your-domain>.lusid.com/notification/api/subscriptions"
-H "Authorization: Bearer <your-API-access-token>"
-H "Content-Type: application/json"
-d '{
"id": {
"scope": "PortfolioEvent",
"code": "AnyPortfolioCreated"
},
"displayName": "Any Portfolio Created",
"description": "Subscribes to all PortfolioCreated events",
"status": "Active",
"matchingPattern": {
"eventType": "PortfolioCreated",
"filter": ""
}
}'
The response is as follows:
{
"id": {
"scope": "PortfolioEvent",
"code": "AnyPortfolioCreated"
},
"displayName": "Any Portfolio Created",
"description": "Subscribes to all PortfolioCreated events",
"status": "Active",
"matchingPattern": {
"eventType": "TransactionPortfolioCreated",
"filter": ""
},
"createdAt": "2023-02-15T10:13:36.8915431+00:00",
"userIdCreated": "00uji4twb7X42sdse2p7",
"modifiedAt": "2023-02-15T10:13:36.8915431+00:00",
"userIdModified": "00uji4twb7X42sdse2p7"
"useAsAuth":"00uji4twb7X42sdse2p7"
"href": "https://<your-domain>.lusid.com/notification/api/subscriptions/PortfolioEvent/AnyPortfolioCreated"
}
You can sign in to the LUSID web app to view the new subscription on the Subscription Management dashboard, available from the top left menu under Jobs & Scheduling:
Step 2: Attaching a webhook notification to the subscription
Now that we have subscribed to the portfolio created event, we can create a webhook notification for the subscription to post to the UpsertTransactions
API each time the event occurs. In this tutorial, we want to seed all newly-created portfolios with 10,000,000 USD and 7,500,000 GBP cash. To do so, we call the CreateNotification API, passing in our API access token and:
The
scope
andcode
of the subscription to attach the notification to. In our case,PortfolioEvent
andAnyPortfolioCreated
.A
displayName
and uniquenotificationId
to refer to the notification once created.A
notificationType
containing:A
Type
ofWebhook
.The
HttpMethod
for the type of request we want to make to the LUSID REST API. In our case, we want to make aPOST
request.The
Url
we want to make a POST request to.A
ContentType
ofJson
.A
Content
object which contains the expected payload for the UpsertTransactions API.
curl -X POST "https://<your-domain>.lusid.com/notification/api/subscriptions/PortfolioEvent/AnyPortfolioCreated/notifications"
-H "Authorization: Bearer <your-API-access-token>"
-H "Content-Type: application/json"
-d '{
"notificationId": "PortfolioCreatedWebhook",
"displayName": "Seed Portfolio With Cash",
"description": "Any portfolio created seed with cash",
"notificationType": {
"Type": "Webhook",
"HttpMethod": "Post",
"Url": "/api/api/transactionportfolios/{{Body.portfolioScope}}/{{Body.portfolioCode}}/transactions",
"AuthenticationType": "Lusid",
"ContentType": "Json",
"Content": [ {
"transactionId": "SeedFundsTransactionUSD",
"type": "FundsIn",
"instrumentIdentifiers": {"Instrument/default/Currency": "USD"},
"transactionDate": "{{Header.timestamp}}",
"settlementDate": "{{Header.timestamp}}",
"units": 10000000,
"transactionPrice": {
"price": 1,
"type": "Price"
},
"totalConsideration": {
"amount": 10000000,
"currency": "USD"
},
"transactionCurrency": "USD",
},
{
"transactionId": "SeedFundsTransactionGBP",
"type": "FundsIn",
"instrumentIdentifiers": {"Instrument/default/Currency": "GBP"},
"transactionDate": "{{Header.timestamp}}",
"settlementDate": "{{Header.timestamp}}",
"units": 7500000,
"transactionPrice": {
"price": 1,
"type": "Price"
},
"totalConsideration": {
"amount": 7500000,
"currency": "GBP"
},
"transactionCurrency": "GBP",
}
]
}'
Step 3: Checking that a new portfolio is automatically seeded with cash
Now that we have created a webhook notification, we can create a new transaction portfolio to check that new portfolios are now automatically seeded with cash. Let's create a new portfolio via the Portfolios dashboard in the LUSID web app with scope FBNUniversity
and code NewTradingPortfolio
​​​​​:
Once the new portfolio has been created, navigate from the top left menu in the web app to Dashboard > Transactions and select the new portfolio. We can see that the webhook notification has successfully seeded our new portfolio with the USD and GBP cash amounts specified earlier: