Skip to main content

TypeScript cookbook

Get code snippets for essential tasks when using the TypeScript SDK to work with Vault

Overview

Find essential code snippets to jumpstart your development using the Vault TypeScript SDK.

Connecting to your Vault

Import the Vault SDK and create an object to interact with your development vault on localhost using the default credentials.

import { VaultClient } from "@piiano/vault-client";
const client = new VaultClient();

When you need to connect to a remote implementation for a particular user.

const client = new VaultClient({
vaultURL: "<host URL>",
apiKey: "<user API key>",
});

Create a collection

Create a collection in your vault to store data about a personal.

await client.collections.addCollection({
requestBody: {
name: "users",
type: "PERSONS",
properties: [
{ name: "name", data_type_name: "NAME" },
{ name: "email", data_type_name: "EMAIL" },
{ name: "phone", data_type_name: "PHONE_NUMBER" },
{ name: "ssn", data_type_name: "SSN" },
],
},
});

Store data in a collection

Store person details in a collection in your vault.

const response = await client.objects.addObject({
collection: "users",
reason: "AppFunctionality",
requestBody: {
name: "John Doe",
email: "john@doe.com",
phone: "+1-123-4567890",
ssn: "444-21-4357".
},
});

Download a working example from the Vault TypeScript SDK GitHub repository.

Tokenize an SSN

Obtain a token for an SSN that you can use to pass details between systems securely.

 const token = await client.tokens.tokenize({
collection: "users",
reason: "AppFunctionality",
requestBody: [
{
object: { fields: { name: "john doe", ssn: "444-21-4357" } },
type: "deterministic",
},
],
});

Download a working example from the Vault TypeScript SDK GitHub repository.

Encrypt a person's details

To encrypt a person's details without storing them in a collection in your vault.

const ciphertext = await client.crypto.encrypt({
collection: "users",
reason: "AppFunctionality",
requestBody: [
{
object: {
fields: {
name: "John Doe",
email: "john@doe.com",
phone: "+1-123-4567890",
},
},
},
],
});

Download a working example from the Vault TypeScript SDK GitHub repository.

See also: