Skip to main content

Overview

The DidsApi provides methods for DID operations including resolution, creation, updates, and management.
import { agent } from './agent'

const { didDocument } = await agent.dids.resolve('did:key:...')

Methods

resolve()

Resolve a DID to a DID document following the W3C DID Resolution specification.
const result = await agent.dids.resolve('did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH')

console.log(result.didDocument)
console.log(result.didResolutionMetadata)
console.log(result.didDocumentMetadata)
didUrl
string
required
The DID or DID URL to resolve
options
DidResolutionOptions
Resolution options
Returns: Promise<DidResolutionResult>

resolveDidDocument()

Resolve a DID to a DID document. Throws an error if resolution fails. Simpler alternative to resolve().
const didDocument = await agent.dids.resolveDidDocument('did:key:...')
didUrl
string
required
The DID or DID URL to resolve
Returns: Promise<DidDocument> Throws: CredoError if resolution fails

create()

Create, register, and store a new DID following the DID Registration specification.
const result = await agent.dids.create({
  method: 'key',
  options: {
    keyType: KeyType.Ed25519,
  },
})

const did = result.didState.did
const didDocument = result.didState.didDocument
options
DidCreateOptions
required
DID creation options
  • method: DID method (e.g., ‘key’, ‘peer’, ‘web’)
  • options: Method-specific options
  • secret: Method-specific secrets
  • didDocument: Optional pre-defined DID document
Returns: Promise<DidCreateResult>

update()

Update an existing DID document following the DID Registration specification.
const result = await agent.dids.update({
  did: 'did:web:example.com',
  didDocument: updatedDocument,
})
options
DidUpdateOptions
required
DID update options
  • did: DID to update
  • didDocument: Updated DID document
  • options: Method-specific options
Returns: Promise<DidUpdateResult>

deactivate()

Deactivate an existing DID following the DID Registration specification.
const result = await agent.dids.deactivate({
  did: 'did:web:example.com',
})
options
DidDeactivateOptions
required
DID deactivation options
  • did: DID to deactivate
  • options: Method-specific options
Returns: Promise<DidDeactivateResult>

import()

Import an existing DID that was created outside of the agent. Creates a DidRecord and optionally stores keys.
await agent.dids.import({
  did: 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH',
  didDocument, // optional
  keys: [
    {
      didDocumentRelativeKeyId: '#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH',
      kmsKeyId: 'key-id-in-kms',
    },
  ],
  overwrite: false,
})
options.did
string
required
The DID to import
options.didDocument
DidDocument
DID document. If not provided, will be resolved.
options.keys
Array<{ didDocumentRelativeKeyId: string, kmsKeyId: string }>
default:"[]"
Keys associated with the DID to store in the wallet
options.overwrite
boolean
default:"false"
Whether to overwrite existing DID record
Returns: Promise<void> Throws: CredoError if DID already exists and overwrite is false

getCreatedDids()

Get a list of all DIDs created by the agent.
const dids = await agent.dids.getCreatedDids()
const keyDids = await agent.dids.getCreatedDids({ method: 'key' })
const specificDid = await agent.dids.getCreatedDids({ did: 'did:key:...' })
options.method
string
Filter by DID method
options.did
string
Filter by specific DID
Returns: Promise<DidRecord[]>

resolveCreatedDidDocumentWithKeys()

Resolve a created DID document along with its associated keys.
const { didDocument, keys } = await agent.dids.resolveCreatedDidDocumentWithKeys(
  'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH'
)

console.log('DID Document:', didDocument)
console.log('Associated keys:', keys)
did
string
required
The DID to resolve
Returns: Promise<{ didDocument: DidDocument, keys: Array<{ didDocumentRelativeKeyId: string, kmsKeyId: string }> }> Throws: RecordNotFoundError if DID was not created by this agent

resolveVerificationMethodFromCreatedDidRecord()

Resolve a verification method from a created DID record.
const { verificationMethod, publicJwk } = 
  await agent.dids.resolveVerificationMethodFromCreatedDidRecord(
    'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH',
    ['authentication', 'assertionMethod']
  )
didUrl
string
required
DID URL referencing the verification method
allowedPurposes
Array<DidPurpose | 'verificationMethod'>
Filter verification methods by purpose
Returns: Promise<{ verificationMethod: VerificationMethod, publicJwk: PublicJwk }>

Properties

supportedResolverMethods

List of DID methods supported for resolution.
const methods = agent.dids.supportedResolverMethods
console.log(methods) // ['key', 'peer', 'web', ...]
Type: string[]

supportedRegistrarMethods

List of DID methods supported for registration (creation).
const methods = agent.dids.supportedRegistrarMethods
console.log(methods) // ['key', 'peer', ...]
Type: string[]

Example Usage

import { Agent, KeyType } from '@credo-ts/core'

// Create a new DID
const createResult = await agent.dids.create({
  method: 'key',
  options: {
    keyType: KeyType.Ed25519,
  },
})

const did = createResult.didState.did
console.log('Created DID:', did)

// Resolve the DID
const { didDocument } = await agent.dids.resolve(did)
console.log('DID Document:', didDocument)

// Get all created DIDs
const allDids = await agent.dids.getCreatedDids()
console.log('Created DIDs:', allDids.map(d => d.did))

// Import an external DID
await agent.dids.import({
  did: 'did:web:example.com',
})

// Resolve with keys
const { didDocument: doc, keys } = 
  await agent.dids.resolveCreatedDidDocumentWithKeys(did)
console.log('Keys:', keys)

Build docs developers (and LLMs) love