Skip to Content
API ReferenceAgent

Agent API

createAgent

Creates a new agent identity with a DID, key pair, and DID Document.

function createAgent(config: AgentConfig): Promise<AgentIdentity>

Parameters

AgentConfig

ParameterTypeRequiredDefaultDescription
domainstringYes---Domain to host the DID Document
pathstringNo---Sub-path for multiple agents on the same domain
algorithm"ES256" | "EdDSA"No"ES256"Signing algorithm
storageStorageAdapterNo---Storage adapter for persisting the identity

Returns

AgentIdentity

FieldTypeDescription
didstringThe DID, e.g. did:web:acme.com
didDocumentDIDDocumentThe DID Document containing the public key
keyPairKeyPairThe cryptographic key pair (contains algorithm, publicKey, privateKey)
domainstringThe domain
pathstring | undefinedThe sub-path, if any

Example

import { createAgent, MemoryStorage } from "@credat/sdk"; const storage = new MemoryStorage(); const agent = await createAgent({ domain: "acme.com", path: "agents/bot-1", algorithm: "EdDSA", storage, }); // agent.did === "did:web:acme.com:agents:bot-1"

Errors

CodeWhen
AGENT_CREATION_FAILEDDomain is missing or empty

loadAgent

Loads a previously created agent identity from storage.

function loadAgent(options: LoadAgentOptions): Promise<AgentIdentity>

Parameters

LoadAgentOptions

ParameterTypeRequiredDescription
didstringYesThe DID of the agent to load
storageStorageAdapterYesStorage adapter where the agent was saved

Returns

AgentIdentity --- same shape as createAgent.

Example

import { loadAgent, MemoryStorage } from "@credat/sdk"; const storage = new MemoryStorage(); const agent = await loadAgent({ did: "did:web:acme.com", storage, });

Errors

CodeWhen
AGENT_NOT_FOUNDNo agent found with this DID in storage

MemoryStorage

In-memory implementation of StorageAdapter. Data is lost when the process exits.

class MemoryStorage implements StorageAdapter

Methods

MethodSignatureDescription
getget<T>(collection: string, key: string): Promise<T | null>Get a value
setset<T>(collection: string, key: string, value: T): Promise<void>Set a value
deletedelete(collection: string, key: string): Promise<boolean>Delete a value
listlist<T>(collection: string): Promise<Array<{ key: string; value: T }>>List all values in a collection
clearclear(collection?: string): Promise<void>Clear a collection or all data

StorageAdapter interface

Implement this interface to use any backing store:

interface StorageAdapter { get<T>(collection: string, key: string): Promise<T | null>; set<T>(collection: string, key: string, value: T): Promise<void>; delete(collection: string, key: string): Promise<boolean>; list<T>(collection: string): Promise<Array<{ key: string; value: T }>>; clear(collection?: string): Promise<void>; }
Last updated on