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

Skip to main content

Send DWN Records

2 minute read

With Web5, records can be:

This guide shows how to use Web5 to handle each scenario.

Write to Local DWN​

The records.create() function creates a record and also writes it to the user's local DWN. You do not need to call the send() function to write a record to a local DWN.

const { record } = await web5.dwn.records.create({
data: "this record will be written to the local DWN",
message: {
dataFormat: 'text/plain'
}
});

The same is true for the protocols.configure() function. It creates the protocol object and installs it on the user's local DWN. You do not need to call the send() method to install a protocol on a local DWN.

const response = await web5.dwn.protocols.configure({
message: {
definition: protocolDefinition
}
});

Send to User's Remote DWNs​

By default, Web5 will automatically sync records and protocols between a user's local and remote DWNs. There is no special action required to accomplish this.

However, sync happens on a predefined interval. If you want a record or protocol immediately sent to the user's remote DWNs, then you can call send(). This will only send that particular record or protocol to the remote DWNs and is not a full sync of the DWNs.

const { record } = await web5.dwn.records.create({
data: "this record will be written to the local DWN",
message: {
dataFormat: 'text/plain'
}
});

//immediately send record to user's remote DWNs
const {status} = await record.send(userDid);
const { protocol } = await web5.dwn.protocols.configure({
message: {
definition: protocolDefinition
}
});

//immediately send protocol to user's remote DWNs
const {status} = await protocol.send(userDid);

Send to Recipient's DWNs​

Assuming permission has been established, one party can send records to another. To do so, the user can create the record and then call send() to send it to another party.

const { record } = await web5.dwn.records.create({
data: "this record will be created but not saved to DWN",
store: false, //remove this line if you want to keep a copy of the record in the sender's DWN
message: {
dataFormat: 'text/plain'
},
});

//send record to recipient's DWN
const {status} = await record.send(recipientDid);

Connect with us on Discord

Submit feedback: Open a GitHub issue

Edit this page: GitHub Repo

Contribute: Contributing Guide