How to add external API call within a template

Dear DAML community,

I am very new to DAML and am trying to understand if it is possible to add a call to an external API within a template?
after quite a bit of searching I couldn’t find any similar query that might help me understand if it is possible or no.

An example use case would be a call to an external authentication API within a template ‘UserAuth’

type UAuthID = ContractId UserAuth

template UserAuth 
    with
        user : Party
        backend : Party
        token : Text
    where 
        signatory backend
        observer user

        controller backend can
            Authenticate : UAuthID
                with
                    newToken : Text
                do
                    -- need to call an external API here to validate token.
                    create this with
                        token = newToken

I wanted to know if this kind of action is possible or not.
Thank you in advance!

1 Like

Hi @seardes , welcome to the Daml forums.

It is not possible to make any external calls from Daml templates. Imagine Daml templates as the schema for a multi-party system in that it tries to capture what happens on the ledger, not on the outside world. It covers the data schema (in the with clause), permissions (in the signatory, observer, and controller clauses), and data transformations/processes (in the do clause).

The transformations/processes are triggered by external stimuli - someone calling the Daml Ledger API. And they result in transactions, which can be streamed and transformed into external actions by integrations. It’s really a CRQS model.

So if you need input from an external system, whatever calls the Daml Ledger API needs to supply those inputs. If you want to trigger an external system as the result of an on-ledger event, you need to write an integration that listens for that event and calls the external system.

5 Likes

Hi @bernhard,
Thank you for your explanation, it cleared up a couple of things for me.
It helped me understand the concept of DAML much better!

3 Likes