Send a Data Message
An IndexationPayload
is a payload type that can be used to attach arbitrary data
and a key index
to a message. To send a payload, you should at least provide the index
. The data part
(as bytes[]
) is optional.
- Java
- Nodejs
- Python
- Rust
- Wasm
You can use the ClientMessageBuilder.withDataString(data: String) function function to attach data to a message.
public static void getDataMessage() {
Client iota = node();
Message message = iota.message().withIndexString("Hello").withDataString("Tangle").finish();
System.out.println("Message sent https://explorer.iota.org/devnet/message/" + message.id());
MessageId[] fetched_message_ids = iota.getMessage().indexString("Hello");
System.out.println("Messages with Hello index: " + Arrays.toString(fetched_message_ids));
}
Output example:
{
"message": {
"networkId":"14379272398717627559",
"parentMessageIds": [
"1a383abbe5f6a6b0899d718975c3119643aa784a68d04075f4e986fd7a0c0e4b",
"6098f889e31911833df7b7839e8b222d701ab496f7dfa1a719087edf4fa7ae52",
"a98b47db4e8254eccc738c968bd35b08a5491e56d6c1a18af298c42bbd8c3a46",
"da6796c0842c08de832c7948fffedc0d5adce372e50a108f26a128dba6096d31"
],
"payload": {
"type":2,
"index":"494f54412e52532042494e44494e47202d204e4f44452e4a53",
"data":"736f6d65207574662062617365642064617461"
},
"nonce":"13835058055282176519"
},
"messageId":"10f59c101cec669b0a0ba163bc777184c7f63455f5e771d42f910a1ba2ad20ff"
}
You can use the MessageSender.data(data: string | Uint8Arra) function function to attach data to a message.
async function run() {
const { ClientBuilder } = require('@iota/client');
// client will connect to testnet by default
const client = new ClientBuilder().build();
const message = await client.message()
.index('IOTA.RS BINDING - NODE.JS')
.data('some utf based data')
.submit();
console.log(message);
}
run()
Output example:
{
"message": {
"networkId": "14379272398717627559",
"parentMessageIds": [
"1a383abbe5f6a6b0899d718975c3119643aa784a68d04075f4e986fd7a0c0e4b",
"6098f889e31911833df7b7839e8b222d701ab496f7dfa1a719087edf4fa7ae52",
"a98b47db4e8254eccc738c968bd35b08a5491e56d6c1a18af298c42bbd8c3a46",
"da6796c0842c08de832c7948fffedc0d5adce372e50a108f26a128dba6096d31"
],
"payload": {
"type": 2,
"index": "494f54412e52532042494e44494e47202d204e4f44452e4a53",
"data": "736f6d65207574662062617365642064617461"
},
"nonce": "13835058055282176519"
},
"messageId": "10f59c101cec669b0a0ba163bc777184c7f63455f5e771d42f910a1ba2ad20ff"
}
You can use the data
parameter in the
Client.message()
function to attach data to a message.
import iota_client
client = iota_client.Client()
# encoding utf string into list of bytes
some_utf_data = "some utf based data".encode("utf8")
message = client.message(
index="some_data_index", data=some_utf_data
)
print(message)
Output example:
{
"message_id": "8d4fa37be3c00691131c2c3e03e7b8b956c9118a2ce4be3a8597d51d82ed2de9",
"network_id": 7712883261355838377,
"parents": [
"3719d308ae14b7ef1ed5a3a1604228e97587b9da487db10bc6e4a4f800083da0",
"4431e2f776db888488728e0aa34c94975e65d6fa74893aa675172af6b9f37257",
"8f9fa84954c58bcfc9acc33ca827b4ea35c2caae88db736399a031120e85eebf",
"f63d416de97e6a9fd1314fbbbbb263f30dff260f3075f9a65e7dfe1f2cc56ce3"
],
"payload": {
"transaction": "None",
"milestone": "None",
"indexation": [
{
"index": "736f6d655f646174615f696e646578",
"data": [115, 111, 109, 101, 32, 117, 116, 102, 32, 98, 97, 115, 101, 100, 32, 100, 97, 116, 97]
}
],
"receipt": "None",
"treasury_transaction": "None"
},
"nonce": 6917529027641573188
}
You can use the
ClientMessageBuilder.with_data(data: Vec<u8>)
function to sent data in a message.
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example 08_data_message --release
use iota_client::{Client, Result};
/// In this example we will send a message without a payload
#[tokio::main]
async fn main() -> Result<()> {
let iota = Client::builder()
.with_node("https://api.lb-0.h.chrysalis-devnet.iota.cafe")?
// .with_permanode("http://18.196.167.57:8000/api/permanode/", None, None)?
.finish()
.await?;
let message = iota
.message()
.with_index("Hello")
.with_data("Tangle".as_bytes().to_vec())
.finish()
.await?;
println!(
"Message sent https://explorer.iota.org/devnet/message/{}\n",
message.id().0
);
let fetched_message_ids = iota.get_message().index("Hello").await.unwrap();
println!("Messages with Hello index: {fetched_message_ids:?}");
Ok(())
}
Output example:
{
"message": {
"networkId": "14379272398717627559",
"parentMessageIds": [
"1a383abbe5f6a6b0899d718975c3119643aa784a68d04075f4e986fd7a0c0e4b",
"6098f889e31911833df7b7839e8b222d701ab496f7dfa1a719087edf4fa7ae52",
"a98b47db4e8254eccc738c968bd35b08a5491e56d6c1a18af298c42bbd8c3a46",
"da6796c0842c08de832c7948fffedc0d5adce372e50a108f26a128dba6096d31"
],
"payload": {
"type": 2,
"index": "494f54412e52532042494e44494e47202d204e4f44452e4a53",
"data": "736f6d65207574662062617365642064617461"
},
"nonce": "13835058055282176519"
},
"messageId": "10f59c101cec669b0a0ba163bc777184c7f63455f5e771d42f910a1ba2ad20ff"
}
You can use the MessageBuilder.data(data: Uint8Array) function function to attach data to a message.
async function run() {
const { ClientBuilder } = require('@iota/client');
// client will connect to testnet by default
const client = new ClientBuilder().build();
const message = await client.message()
.index('IOTA.RS BINDING - NODE.JS')
.data('some utf based data')
.submit();
console.log(message);
}
run()
Output example:
{
"message": {
"networkId": "14379272398717627559",
"parentMessageIds": [
"1a383abbe5f6a6b0899d718975c3119643aa784a68d04075f4e986fd7a0c0e4b",
"6098f889e31911833df7b7839e8b222d701ab496f7dfa1a719087edf4fa7ae52",
"a98b47db4e8254eccc738c968bd35b08a5491e56d6c1a18af298c42bbd8c3a46",
"da6796c0842c08de832c7948fffedc0d5adce372e50a108f26a128dba6096d31"
],
"payload": {
"type": 2,
"index": "494f54412e52532042494e44494e47202d204e4f44452e4a53",
"data": "736f6d65207574662062617365642064617461"
},
"nonce": "13835058055282176519"
},
"messageId": "10f59c101cec669b0a0ba163bc777184c7f63455f5e771d42f910a1ba2ad20ff"
}
- You can find the message using its
message ID
in the Tangle explorer. - There are three prepared payloads(
transaction
,milestone
, andindexation
). However, at the time, theindexation
payload is the only one in use. data
contains arbitrary data encoded in bytes.- Please note there is no IOTA address involved while sending data messages. Data messages are referenced using their
message ID
orindex
key. - IOTA addresses are part of the
UTXO
data structure that is sent using theSignedTransaction
payload.