SecretVaults SDK TypeScript Docs
If you are using secretvaults-ts, please upgrade to at least version 0.1.7
This guide shows how to get started building with the Nillion secretvaults-ts SDK to build applications with Nillion Private Storage.
Install Nillion Dependencies
npm install @nillion/secretvaults @nillion/nuc
Builder Client Usage
SecretVaults SDK SecretVaultBuilderClient Usage
Prerequisites
Before building with the SecretVaults SDK, you need:
- Nillion Wallet: Create a wallet
- Testnet NIL: Get NIL tokens from the Nillion faucet
- Get a Nillion API Key with a subscription to nilDB
Initialize a Builder Client
Initialize the client with your Nillion API Key and a valid nilDB subscription. Connect to nilDB nodes of choice. Call refreshRootToken() after initialization to obtain authentication tokens.
- builder keypair: from your Nillion API Key
- urls: nilDB Network Config
- blindfold.operation: Set to 'store' operation for storing data
loading...
Register Builder Profile
One-time setup to register your builder profile to the nilDB nodes. Checks if profile exists, creates one if needed, and handles duplicate registration errors.
- did: Decentralized identifier generated from your API key
- name: Display name that will be set for your builder profile
loading...
Create Collection
Collections organize and validate your data according to the schema rules. Builders can create "standard" or "owned" collection types.
You can use the Collection Explorer Tool to more easily build and validate a JSON schema for your collection.
Create Standard Collection
- Standard Collection
- contactBookSchema
loading...
loading...
Create Owned Collection
- Owned Collection
- contactBookSchema
loading...
loading...
Read Collections
Gets the id, collection type, and name of all collections.
loading...
Create Records
Creates new records in a standard collection. Records must match the collection's JSON schema. Use %allot to mark fields for encryption.
- collection: ID of the target collection
- data: Array of record objects matching the schema
- %allot: Special field marker that encrypts the value
Use the no-code Collection Explorer Tool to view the collection and schema
The collection page has an "Example Record Payload" button that will show you the data structure for a record in the collection.
loading...
Read Records
Finds and retrieves records from a collection using filter criteria. Use the collection explorer to view collections and records.
- collection: ID of the collection to search
- filter: Query object to match records (e.g., by _id or other fields)
loading...
Update Records
Updates existing records in a collection. Can modify both regular and encrypted fields using MongoDB-style update operators.
- collection: ID of the target collection
- filter: Query to match records for updating
- update: Update operations using $set, $unset, etc.
loading...
Delete Records
Deletes records from a collection based on filter criteria. Use the collection explorer to verify deletions.
- collection: ID of the target collection
- filter: Query to match records for deletion
loading...
Create Query
Creates a new saved query in the system using MongoDB-style aggregation pipeline syntax. Queries operate on plaintext fields only - they cannot query encrypted fields (those marked as secret). Queries can be used for simple filtering or complex operations like statistical analysis, data aggregation, and transformations. Once saved, queries can be parameterized with variables and reused across different executions.
Queries support the full range of MongoDB aggregation operations on plaintext fields - from simple $match filters to complex statistical calculations using $group, $avg, $min, $max, and more. Encrypted fields cannot be queried. Reference the Query Examples section to see how to structure aggregation pipelines for string and number field queries.
- Create Age Query
- contactBookSchema
This creates a query for a collection with a plaintext age number field. You could create this query for a Contact Book Collection, which has age as a plaintext, optional number field.
const queryId = randomUUID();
const queryRequest: CreateQueryRequest = {
_id: queryId,
collection: collectionId,
name: 'Calculate Age Statistics',
variables: {},
pipeline: [
{
$match: {
age: { $exists: true, $ne: null },
},
},
{
$group: {
_id: null,
averageAge: { $avg: '$age' },
minAge: { $min: '$age' },
maxAge: { $max: '$age' },
totalWithAge: { $sum: 1 },
standardDeviation: { $stdDevPop: '$age' },
},
},
],
};
const createResponse = await builderClient.createQuery(queryRequest);
loading...
Run Query
Executes a saved query and retrieves the results. Query execution is asynchronous across distributed nodes, requiring polling to get the final results. You can pass variables to parameterize the query at runtime.
- _id: ID of the saved query to execute
- variables: Object containing values for any query variables defined in the saved query
- Polling: Results must be polled as queries execute asynchronously across nodes
- Timeout: Example shows polling for up to 30 seconds with exponential backoff
loading...