Skip to main content

Overview

The DIDComm module provides APIs for working with DIDComm protocols, enabling secure, encrypted messaging between agents. Available when the DIDComm module is registered:
import { DidCommModule } from '@credo-ts/didcomm'
import { Agent } from '@credo-ts/core'

const agent = new Agent({
  // ...
  modules: {
    didcomm: new DidCommModule(),
  },
})

await agent.initialize()

// Access DIDComm APIs
const connections = agent.didcomm.connections
const credentials = agent.didcomm.credentials  
const proofs = agent.didcomm.proofs

ConnectionsApi

Manage DIDComm connections between agents.

acceptOutOfBandInvitation()

Accept an out-of-band invitation to establish a connection.
// Parse invitation
const invitation = await agent.didcomm.oob.parseInvitation(invitationUrl)
const oobRecord = await agent.didcomm.oob.receiveInvitation(invitation)

// Accept invitation
const connectionRecord = await agent.didcomm.connections.acceptOutOfBandInvitation(
  oobRecord,
  {
    label: 'My Agent',
    protocol: DidCommHandshakeProtocol.DidExchange,
    autoAcceptConnection: true,
  }
)

console.log('Connection:', connectionRecord.id)
outOfBandRecord
DidCommOutOfBandRecord
required
Out-of-band record from received invitation
config
object
required
Configuration:
  • label: Agent label
  • protocol: Handshake protocol (‘did-exchange’ or ‘connections’)
  • autoAcceptConnection: Auto-accept connection
  • alias: Optional connection alias
  • routing: Optional routing configuration
Returns: Promise<DidCommConnectionRecord>

acceptRequest()

Accept a connection request (as inviter).
const connectionRecord = await agent.didcomm.connections.acceptRequest(
  connectionId
)
connectionId
string
required
Connection record ID
Returns: Promise<DidCommConnectionRecord>

acceptResponse()

Accept a connection response (as invitee).
const connectionRecord = await agent.didcomm.connections.acceptResponse(
  connectionId
)
connectionId
string
required
Connection record ID
Returns: Promise<DidCommConnectionRecord>

sendPing()

Send a trust ping to test connection.
const message = await agent.didcomm.connections.sendPing(connectionId, {
  responseRequested: true,
})
connectionId
string
required
Connection ID
options
SendPingOptions
  • responseRequested: Request response ping
  • withReturnRouting: Use return routing
Returns: Promise<TrustPingMessage>

getAll()

Retrieve all connections.
const connections = await agent.didcomm.connections.getAll()
Returns: Promise<DidCommConnectionRecord[]>

getById()

Retrieve connection by ID.
const connection = await agent.didcomm.connections.getById(connectionId)
connectionId
string
required
Connection ID
Returns: Promise<DidCommConnectionRecord>

findAllByQuery()

Find connections by query.
const connections = await agent.didcomm.connections.findAllByQuery(
  { state: DidCommConnectionState.Completed },
  { limit: 10 }
)
query
Query<DidCommConnectionRecord>
required
Query object
queryOptions
QueryOptions
Query options (limit, offset)
Returns: Promise<DidCommConnectionRecord[]>

deleteById()

Delete a connection.
await agent.didcomm.connections.deleteById(connectionId)
connectionId
string
required
Connection ID
Returns: Promise<void>

CredentialsApi

Issue and receive credentials using DIDComm protocols.

offerCredential()

Offer a credential to a connection (issuer).
const credentialRecord = await agent.didcomm.credentials.offerCredential({
  connectionId,
  protocolVersion: 'v2',
  credentialFormats: {
    anoncreds: {
      credentialDefinitionId,
      attributes: [
        { name: 'name', value: 'Alice Smith' },
        { name: 'degree', value: 'Bachelor of Science' },
      ],
    },
  },
  autoAcceptCredential: AutoAcceptCredential.ContentApproved,
})
options.connectionId
string
required
Connection ID
options.protocolVersion
string
required
Protocol version (‘v1’ or ‘v2’)
options.credentialFormats
object
required
Format-specific credential data
options.autoAcceptCredential
AutoAcceptCredential
Auto-accept behavior
Returns: Promise<DidCommCredentialExchangeRecord>

acceptOffer()

Accept a credential offer (holder).
const credentialRecord = await agent.didcomm.credentials.acceptOffer({
  credentialExchangeRecordId,
  autoAcceptCredential: AutoAcceptCredential.Always,
})
options.credentialExchangeRecordId
string
required
Credential exchange record ID
options.credentialFormats
object
Format-specific options
Returns: Promise<DidCommCredentialExchangeRecord>

acceptRequest()

Accept a credential request and issue credential (issuer).
const credentialRecord = await agent.didcomm.credentials.acceptRequest({
  credentialExchangeRecordId,
  credentialFormats: {
    anoncreds: {
      // Final credential values
    },
  },
})
options.credentialExchangeRecordId
string
required
Credential exchange record ID
options.credentialFormats
object
Format-specific credential data
Returns: Promise<DidCommCredentialExchangeRecord>

acceptCredential()

Accept a received credential (holder).
const credentialRecord = await agent.didcomm.credentials.acceptCredential({
  credentialExchangeRecordId,
})
options.credentialExchangeRecordId
string
required
Credential exchange record ID
Returns: Promise<DidCommCredentialExchangeRecord>

getAll()

Retrieve all credential exchange records.
const records = await agent.didcomm.credentials.getAll()
Returns: Promise<DidCommCredentialExchangeRecord[]>

getById()

Retrieve credential exchange record by ID.
const record = await agent.didcomm.credentials.getById(credentialExchangeRecordId)
credentialExchangeRecordId
string
required
Credential exchange record ID
Returns: Promise<DidCommCredentialExchangeRecord>

ProofsApi

Request and present proofs using DIDComm protocols.

requestProof()

Request a proof from a connection (verifier).
const proofRecord = await agent.didcomm.proofs.requestProof({
  connectionId,
  protocolVersion: 'v2',
  proofFormats: {
    anoncreds: {
      name: 'Employment Verification',
      version: '1.0',
      requested_attributes: {
        name: {
          name: 'name',
          restrictions: [{ cred_def_id: credDefId }],
        },
      },
      requested_predicates: {
        age: {
          name: 'age',
          p_type: '>=',
          p_value: 18,
          restrictions: [{ cred_def_id: credDefId }],
        },
      },
    },
  },
})
options.connectionId
string
required
Connection ID
options.protocolVersion
string
required
Protocol version (‘v1’ or ‘v2’)
options.proofFormats
object
required
Format-specific proof request
Returns: Promise<DidCommProofExchangeRecord>

acceptRequest()

Accept a proof request and send presentation (prover).
// Get credentials for request
const credentials = await agent.didcomm.proofs.getCredentialsForRequest({
  proofExchangeRecordId,
})

// Select credentials
const selectedCredentials = await agent.didcomm.proofs.selectCredentialsForRequest({
  proofExchangeRecordId,
})

// Accept and send proof
const proofRecord = await agent.didcomm.proofs.acceptRequest({
  proofExchangeRecordId,
  proofFormats: selectedCredentials.proofFormats,
})
options.proofExchangeRecordId
string
required
Proof exchange record ID
options.proofFormats
object
required
Format-specific proof data
Returns: Promise<DidCommProofExchangeRecord>

acceptPresentation()

Accept and verify a received presentation (verifier).
const proofRecord = await agent.didcomm.proofs.acceptPresentation({
  proofExchangeRecordId,
})

if (proofRecord.state === DidCommProofState.Done) {
  console.log('Proof verified successfully')
}
options.proofExchangeRecordId
string
required
Proof exchange record ID
Returns: Promise<DidCommProofExchangeRecord>

getCredentialsForRequest()

Get available credentials for a proof request.
const credentials = await agent.didcomm.proofs.getCredentialsForRequest({
  proofExchangeRecordId,
})

console.log('Available credentials:', credentials)
options.proofExchangeRecordId
string
required
Proof exchange record ID
Returns: Promise<GetCredentialsForProofRequestReturn>

selectCredentialsForRequest()

Automatically select credentials for a proof request.
const selected = await agent.didcomm.proofs.selectCredentialsForRequest({
  proofExchangeRecordId,
})
options.proofExchangeRecordId
string
required
Proof exchange record ID
Returns: Promise<SelectCredentialsForProofRequestReturn>

getAll()

Retrieve all proof exchange records.
const records = await agent.didcomm.proofs.getAll()
Returns: Promise<DidCommProofExchangeRecord[]>

getById()

Retrieve proof exchange record by ID.
const record = await agent.didcomm.proofs.getById(proofExchangeRecordId)
proofExchangeRecordId
string
required
Proof exchange record ID
Returns: Promise<DidCommProofExchangeRecord>

Complete Example

import { Agent, AutoAcceptCredential } from '@credo-ts/core'
import { DidCommModule, DidCommHandshakeProtocol } from '@credo-ts/didcomm'

// Setup agents
const issuer = new Agent({
  // ...
  modules: { didcomm: new DidCommModule() },
})

const holder = new Agent({
  // ...
  modules: { didcomm: new DidCommModule() },
})

await issuer.initialize()
await holder.initialize()

// Create out-of-band invitation
const oobRecord = await issuer.didcomm.oob.createInvitation({
  label: 'Issuer Agent',
  handshakeProtocols: [DidCommHandshakeProtocol.DidExchange],
})

const invitationUrl = oobRecord.outOfBandInvitation.toUrl({ domain: 'https://example.com' })

// Holder accepts invitation
const holderOobRecord = await holder.didcomm.oob.receiveInvitation(invitationUrl)
const connection = await holder.didcomm.connections.acceptOutOfBandInvitation(
  holderOobRecord,
  {
    label: 'Holder Agent',
    protocol: DidCommHandshakeProtocol.DidExchange,
    autoAcceptConnection: true,
  }
)

// Wait for connection to be established
await holder.didcomm.connections.returnWhenIsConnected(connection.id)

// Issue credential
const credentialRecord = await issuer.didcomm.credentials.offerCredential({
  connectionId: oobRecord.connectionId,
  protocolVersion: 'v2',
  credentialFormats: {
    anoncreds: {
      credentialDefinitionId,
      attributes: [
        { name: 'name', value: 'Alice Smith' },
        { name: 'degree', value: 'Bachelor of Science' },
      ],
    },
  },
  autoAcceptCredential: AutoAcceptCredential.ContentApproved,
})

// Request proof
const proofRecord = await issuer.didcomm.proofs.requestProof({
  connectionId: oobRecord.connectionId,
  protocolVersion: 'v2',
  proofFormats: {
    anoncreds: {
      name: 'Degree Verification',
      version: '1.0',
      requested_attributes: {
        degree: {
          name: 'degree',
          restrictions: [{ cred_def_id: credentialDefinitionId }],
        },
      },
    },
  },
})

Build docs developers (and LLMs) love