.. and press ENTER to ask a question on web5, how to write code and more.

Skip to main content

Web5

caution

WEB5 JS SDK IS CURRENTLY IN TECH PREVIEW

The SDK is currently still under active development, but having entered the Tech Preview phase, there is now a drive to avoid unnecessary changes unless backwards compatibility is provided. Additional functionality will be added in the lead up to version 1.0, and modifications will be made to address issues and community feedback.


Install

npm install @web5/api

Import

Node 19+
import { Web5 } from '@web5/api';
Node <=18
/*
Needs globalThis.crypto polyfill.
This is *not* the crypto you're thinking of.
It's the original crypto...CRYPTOGRAPHY.
*/
import { webcrypto } from "node:crypto";
import { Web5 } from '@web5/api';

// @ts-ignore
if (!globalThis.crypto) globalThis.crypto = webcrypto;
React Native
/*
React Native needs crypto.getRandomValues polyfill and sha512.
This is *not* the crypto you're thinking of.
It's the original crypto...CRYPTOGRAPHY.
*/
import "react-native-get-random-values";
import { hmac } from "@noble/hashes/hmac";
import { sha256 } from "@noble/hashes/sha256";
import { sha512 } from "@noble/hashes/sha512";
import { Web5 } from '@web5/api';

ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
ed.etc.sha512Async = (...m) => Promise.resolve(ed.etc.sha512Sync(...m));

secp.etc.hmacSha256Sync = (k, ...m) =>
hmac(sha256, k, secp.etc.concatBytes(...m));
secp.etc.hmacSha256Async = (k, ...m) =>
Promise.resolve(secp.etc.hmacSha256Sync(k, ...m));

Methods


connect(options)

Enables an app to request connection to a user's local identity app (like a desktop or mobile agent - work is underway for reference apps of each), or generate an in-app DID to represent the user (e.g. if the user does not have an identity app)

note

The outputs of this method invocation will be used throughout the other API methods.

Parameters

options
Web5ConnectOptions
optional
Show parameters


agent
Web5Agent
optional
An instance of a Web5Agent implementation. Defaults to creating a local Web5UserAgent if not provided.
appData
AppDataStore
optional
An instance of an AppDataStore implementation. Defaults to a LevelDB-backed store with an insecure, static unlock passphrase if not provided. To allow the end user to enter a secure passphrase of their choosing, provide an initialized AppDataVault.
connectedDid
string
optional
An existing DID to connect to.
sync
string
optional
Enable or disable synchronization of DWN records between local and remote DWNs. Sync defaults to running every 2 minutes and can be set to any value accepted by ms (https://www.npmjs.com/package/ms). To disable sync set to 'off'
techPreview
TechPreviewOptions
optional
Show parameters


dwnEndpoints
string[]
optional
a list of DWN endpoints to define in the DID created and returned by this method. If this property is omitted during the Tech Preview, two nodes will be included by default (e.g. https://dwn.tbddev.org/dwn0, https://dwn.tbddev.org/dwn3)

Return Value

web5
Web5
A class instance of Web5 that enables access to a locally running DWN, DID interaction methods, and other capabilities related to the connected DID
did
DidApi
A class instance representing the decentralized identifier that was created or attained connection to

Code Examples

const { web5, did: myDid } = await Web5.connect();
Specify a Decentralized Web Node endpoint
const { web5, did: myDid } = await Web5.connect({
techPreview: {
dwnEndpoints: ["https://dwn.your-domain.org/"],
},
});
Connect to Web5 with an exiting agent and existing DID
const web5 = new Web5({
agent: identityAgent,
connectedDid: 'did:ion:EiD7Q...Q',
});

Connect with us on Discord

Submit feedback: Open a GitHub issue

Edit this page: GitHub Repo

Contribute: Contributing Guide