Skip to main content

Post a Data Block

You can post a data block by adding hex encoded data as options when you create a block.

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Create the options with the hex encoded data.
  3. Create a SecretManager from a mnemonic.
  4. Create and post the block with the data from step 2.
  5. Generate a public address.
Iota.js

You can also find this guide in the native iota.js library

Post a Block

Code Example

Dotenv

This example uses dotenv, which is not safe to use in production environments.

// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Calls `POST /api/core/v2/blocks`.
//! Submits a block as a JSON payload.
//! Run: `cargo run --example node_api_core_post_block --release -- [NODE URL]`.

use iota_client::{Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args().nth(1).unwrap_or_else(|| {
// This example uses dotenv, which is not safe for use in production.
dotenv::dotenv().ok();
std::env::var("NODE_URL").unwrap()
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;

// Create the block.
let block = client.block().finish().await?;
// Post the block.
let block_id = client.post_block(&block).await?;

println!("Posted: {block_id:?}");

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example node_api_core_post_block --release -- https://api.testnet.shimmer.network
  • You can replace https://api.testnet.shimmer.network with any node url.

Expected Output

Posted: BlockId(0x2f1de84a2977afeb413d44d8069c67e2df3f8da9ee6ba9dd62190e5256469169)

Post a Raw Block

Code Example

Dotenv

This example uses dotenv, which is not safe to use in production environments.

// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Calls `POST /api/core/v2/blocks`.
//! Submits a block as raw bytes.
//! Run: `cargo run --example node_api_core_post_block_raw --release -- [NODE URL]`.

use iota_client::{Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args().nth(1).unwrap_or_else(|| {
// This example uses dotenv, which is not safe for use in production.
dotenv::dotenv().ok();
std::env::var("NODE_URL").unwrap()
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;

// Create the block.
let block = client.block().finish().await?;
// Post the block as raw bytes.
let block_id = client.post_block_raw(&block).await?;

println!("Posted: {block_id:?}");

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example node_api_core_post_block_raw --release -- https://api.testnet.shimmer.network
  • You can replace https://api.testnet.shimmer.network with any node url.

Expected Output

Posted: BlockId(0x9982248af2d1b41f5f9f5b439d113ebe9611b6ace17ae487f2b3104b00b9950e)