Skip to main content

Quick start guide

This guide will walk you through the process of creating and verifying a verifiable credential (VC) using the Truvity SDK. You will utilize User Defined Types (UDTs) to represent the credential claims schema, enhancing type safety and simplifying the credential lifecycle.

Prerequisites

Before you begin, make sure you have the following:

  • Installed and configured Truvity SDK. If you do not have the installed SDK, follow the Installation guide.
  • A basic understanding of Self-Sovereign Identity (SSI) concepts, such as Decentralized Identifiers (DIDs), Verifiable Credentials (VCs), and Verifiable Presentations (VPs). For more information, refer to the Core Concepts section.

Step 1: Define a simple credential type

Let's begin by defining a simple credential type using UDTs. This credential type contains two required claims: name and age.

import { Truvity, VcContext, VcClaim } from '@truvity/sdk';

// Define a simple credential type using UDT
@VcContext({ name: 'SimpleCredential', namespace: 'https://example.com/credentials' })
class SimpleCredential {
@VcClaim
name?: string;

@VcClaim
age?: number;
}

The @VcContext decorator defines the credential type and its namespace. The @VcClaim decorator marks the fields as claims in the credential.

Step 2: Generate a key for signing

To issue a credential, you first need to generate a cryptographic key. In this case, we are using the ED25519 key type.

const key = await client.keys.keyGenerate({ data: { type: 'ED25519' } });

This key will be used to sign the credential later in the process.

Step 3: Create a draft

Now, let's create a draft using the SimpleCredential UDT that we defined in Step 1. This draft includes the claims (name and age) that we want to embed in the credential.

info

Draft is a concept introduced by Truvity that represents the initial state of credential's content. To learn more about drafts, check the Drafts vs credentals page.

const simpleCredDecorator = client.createVcDecorator(SimpleCredential);

const draft = await simpleCredDecorator.create({
claims: {
name: "Alice",
age: 30
}
});

In this step, the credential is not yet issued or signed — it is just a draft that contains the claims data.

Step 4: Issue the credential

Next, we issue the credential by signing it with the previously generated key. This transforms the draft into a verifiable credential (VC).

const issuedVC = await draft.issue(key.id);

The issued credential is now cryptographically signed and can be shared with other parties.

Step 5: Verify the credential

Finally, verify the authenticity of the issued credential. Verification ensures that the credential has not been tampered with and that it was signed by a valid key.

const verificationResult = await issuedVC.verify();
console.log("Verification result:", verificationResult.verified);

The verify() function checks the integrity and validity of the credential, and the result is printed to the console.

Putting it all together

Now that we have broken down each step, here is the full code that puts everything together. This code defines a credential, generates a key, creates a draft, issues the credential, and finally verifies it.

import { TruvityClient, VcContext, VcClaim } from '@truvity/sdk';

// Define a simple credential type using UDT
@VcContext({ name: 'SimpleCredential', namespace: 'https://example.com/credentials' })
class SimpleCredential {
@VcClaim
name!: string;

@VcClaim
age!: number;
}

async function createAndVerifyVC() {
// Initialize the SDK client
const client = new Truvity.TruvityClient({
environment: "https://api.truvity.cloud",
apiKey: "YOUR_API_KEY",
});

// Generate a key for signing
const key = await client.keys.keyGenerate({ data: { type: 'ED25519' } });

// Create a credential decorator
const simpleCredDecorator = client.createVcDecorator(SimpleCredential);

// Create a draft
const draft = await simpleCredDecorator.create({
claims: {
name: "Alice",
age: 30
}
});

// Issue the credential
const issuedVC = await draft.issue(key.id);

// Verify the credential
const verificationResult = await issuedVC.verify();
console.log("Verification result:", verificationResult.verified);
}

createAndVerifyVC();

Congratulations! You have just issued your first Truvity-powered verifiable credential and is now ready to explore more topics on working with the SDK.

Further reading