A non-official Node.js client for MATTR VII API platform
Install the SDK
yarn add mattr-vii-client
// or
npm install mattr-vii-client
π Import the SDK
import { MattrViiClient, ApiTypes } from 'mattr-vii-client'
π₯ Initialise the client
const client = new MattrViiClient({
tenantSubdomain: process.env.MATTR_TENANT_URL,
authToken: process.env.MATTR_AUTH_TOKEN,
});
// mattr.service.ts
@Injectable()
export class MattrService extends MattrViiClient {
super({
tenantSubdomain: process.env.MATTR_TENANT_URL,
authToken: process.env.MATTR_AUTH_TOKEN,
})
}
// user.controller.ts
@Controller('user')
export class UserController {
constructor(
private readonly mattrService: MattrService,
private readonly prismaService: PrismaService,
) {};
@Post('create')
public async createUser(@Body() args: CreateUserBody) {
const body: ApiTypes.PlatformCore.DIDs.CreateDidReqBody = {
method: 'key',
options: {
keyType: 'ed25519',
},
};
const did = await this.mattrService.PlatformCore.DIDs.createDid(body);
return await this.prismaService.user.create({
...args,
did: did.did,
})
}
}
/*
Example use-case:
=== Send a verifiable credential to a Digital Wallet ===
Main steps:
1. Create a DID
2. Create a Credential using the DID created
3. Sign a message using the Credential you just created
4. Encrypt the signed message
5. Send the message to a wallet
*/
// π© Before π©
// β Raw API calls
const url = `https://${process.env.MATTR_TENANT}.vii.mattr.global/core/v1`;
const createDid = async () => {
const res = await fetch(`${url}/dids`, {
method: "post",
headers: {
"Content-type": "application/json",
Authorization: 'Bearer <YOUR_JWT_HERE>',
},
// β You have no idea if the body you passed in is what the endpoint expects
body: JSON.stringify({
method: 'key',
options: {keyType: 'ed25519'},
}),
});
return res.json();
// β You have no clue how big & nested your response will be...
}
// β
Now with our SDK β
/* main.ts */
import { MattrViiClient, ApiTypes } from 'mattr-vii-client';
// β
Initialise the SDK once and use it across your backend
// β
Minimal environment variables required
const client = new MattrViiClient({
tenantSubdomain: process.env.MATTR_TENANT_URL,
authToken: process.env.MATTR_AUTH_TOKEN,
});
export const createDid = async () => {
// β
Tells you exactly the shape of your request body
const body: ApiTypes.PlatformCore.DIDs.CreateDidReqBody = {
method: 'key',
options: {
keyType: 'ed25519',
},
};
// β
Auto-completion helps you figuring out which method to call
// β
Enforcing you to pass in the correct body for each request
// β
Has a response type
return await client.PlatformCore.DIDs.createDid({ body });
}