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

Skip to main content

Write DWN Records

Various data types, such as text, JSON, images, streams or files, can be stored as records in a DWN. Below are examples of each.

Create a text record​

const { record } = await web5.dwn.records.create({
data: 'Hello, Web5!',
message: {
dataFormat: 'text/plain',
},
});

Create a JSON record​

// Create a JSON record
const { record } = await web5.dwn.records.create({
data: {
content: "Hello Web5",
description: "Keep Building!"
},
message: {
dataFormat: 'application/json'
}
});

Create a blob or image record​

// Create a blob record
async function upload(event) {
const blob = new Blob(event.currentTarget.files, { type: "image/png" });
const { record } = await web5.dwn.records.create({
data: blob,
message: {
dataFormat: "image/png"
}
});

}

Create a file record​

// Create a file record
async function upload(event) {
const file = event.currentTarget.files[0];
const { status: fileStatus, record } = await web5.dwn.records.create({
data: file,
message: {
schema: "https://schema.org/path/to/schema",
dataFormat: "application/octet-stream"
}
});

}

Create records with mixed data​

You can also store mixed data via JSON - including text, images, or files.

// Create a mixed record
async function createMessage(username, messageText, imageFile) {
let base64Image = null;

if (imageFile) {
const binaryImage = await imageFile.arrayBuffer();
base64Image = btoa(
new Uint8Array(binaryImage).reduce(
(data, byte) => data + String.fromCharCode(byte),
""
)
);
}

const messageData = {
username,
message: messageText,
image: base64Image
};

const { record } = await web5.dwn.records.create({
data: messageData,
message: {
schema: "http://schema-registry.org/message",
dataFormat: "application/json"
},
});

}

For more information on writing records to DWNs, see our API Guide.

Connect with us on Discord

Submit feedback: Open a GitHub issue

Edit this page: GitHub Repo

Contribute: Contributing Guide