Note: the pre-built example in Typescript is available on our
Github
1
Setup the environment
For this example, we’ll use a basic React app. Let’s first prepare the environment: runand answer the few questions in order to get set. You can then run the server withThen copy-paste the base page for starter:
src/App.js
2
Registering a new credential
WebAuthn credentials, that Dfns uses for logging you in and signing all your requests, are platform-specific. In particular, you cannot reuse the dashboard credentials for another front-end. In this case we are using localhost as a front-end, so we need to create a dedicated credentials for it.
There is an easy way to enable this use case from the dashboard: head to your credentials and click “Get Cred. Code”. This code will then be usage to register a new credentials from a new front-end. That could be a new device, but in our case it’s just a new URL. This code is valid for 60 seconds only so you will have to request it only when you are ready.
We have already prepared the function for you to fill-in:As explained in Create Credential With Code Flow, in order to register the credentials we need toRun this code and your browser will ask you to create a new passkey for your localhost website. You only need to do this once, as later your browser will offer to use the 🔑 passkey automatically.
Now you can also check on the dashboard in Settings > Credentials, your new credential (default name is “DemoLogin”) has been registered!
- Get a challenge code using
POST /auth/credentials/code/init
- Create a credentials from our browser based on the challenge we received. This steps requires some adaptation as Dfns send Base64URL-encoded strings when the browsers expect ArrayBuffers, hence the length of this code:
- Register those new credentials using
POST /auth/credentials/code/verify
3
Login using your 🔑 Passkey
In a very similar way, as explained in login, you need to request a Bearer token for authenticating all your API requests.
The placeholder is there for you already:For this we need to:Run this code and your browser will ask you to sign in with the passkey created earlier for your localhost website. You then get a token! Hooray!
Now you can use the Bearer token in a
- Get a “User Login Challenge” using
POST /auth/login/init
- Get the browser to 🔑 sign the challenge to prove identity. This steps requires some adaptation as Dfns send Base64URL-encoded strings when the browsers expect ArrayBuffers, hence the length of this code. 💡 We recommend creating a function for this, as you will need to reuse it for all your API requests later.
- Verify the signature and get a Bearer token using
POST /auth/login
Authorization header in all your requests.4
First API request: read the wallet list
Use your Bearer token to list the wallets:
- JavaScript
- Shell
Congratulations! you are already able to query data from your account after login in!
5
User Actions: signing requests
For any action that’s modifying things in your account (e.g.: creating a wallet), Dfns asks for a signature, which is done by using your 🔑 passkey as earlier with the login. As before this process requires multiple steps:Awesome! you got a token! note that it is only usable once so you will have to request a new one (and sign that request) every time you need to do an action on your account.
- Get a “User Action Signature Challenge” from
POST /auth/action/init. See details on inituseractionsigning.
- Sign the challenge
- Return to the Dfns system and request a “User Action Token” using
POST /auth/actionand get the the token!
- Include the new token on top of your login token in your requests
Congratulations! You can now make server-side API calls using your service account. Now start building your app using our Typescript SDK and specifically the Service Account sample app.
