Skip to main content

Build an Alias Output

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Create a SecretManager from a mnemonic.
  3. Build an Alias output.

Code Example


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

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

//! cargo run --example alias --release

use iota_client::{
feature::{IssuerFeature, MetadataFeature, SenderFeature},
GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition, UnlockCondition,
AliasId, AliasOutputBuilder, Feature, Output, OutputId,
payload::{transaction::TransactionEssence, Payload},
secret::{mnemonic::MnemonicSecretManager, SecretManager},
Client, Result,

/// In this example we will create an alias output

async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production!
// Configure your own mnemonic in the ".env" file. Since the output amount cannot be zero, the seed must contain
// non-zero balance.

let node_url = std::env::var("NODE_URL").unwrap();
let faucet_url = std::env::var("FAUCET_URL").unwrap();

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

let secret_manager = SecretManager::Mnemonic(MnemonicSecretManager::try_from_mnemonic(

let token_supply = client.get_token_supply().await?;

let address = client.get_addresses(&secret_manager).with_range(0..1).get_raw().await?[0];
request_funds_from_faucet(&faucet_url, &address.to_bech32(client.get_bech32_hrp().await?)).await?;

// create new alias output
let alias_output_builder = AliasOutputBuilder::new_with_amount(1_000_000, AliasId::null())?
.add_feature(Feature::Metadata(MetadataFeature::new(vec![1, 2, 3])?))

let outputs = vec![alias_output_builder.clone().finish_output(token_supply)?];

let block = client

"Transaction with new alias output sent: {node_url}/api/core/v2/blocks/{}",
let _ = client.retry_until_included(&, None, None).await?;

// create second transaction with the actual AliasId (BLAKE2b-160 hash of the Output ID that created the alias)
let alias_output_id = get_alias_output_id(block.payload().unwrap())?;
let alias_id = AliasId::from(&alias_output_id);
let outputs = vec![

let block = client
"Transaction with alias id set sent: {node_url}/api/core/v2/blocks/{}",
let _ = client.retry_until_included(&, None, None).await?;

// helper function to get the output id for the first alias output
fn get_alias_output_id(payload: &Payload) -> Result<OutputId> {
match payload {
Payload::Transaction(tx_payload) => {
let TransactionEssence::Regular(regular) = tx_payload.essence();
for (index, output) in regular.outputs().iter().enumerate() {
if let Output::Alias(_alias_output) = output {
return Ok(OutputId::new(, index.try_into().unwrap())?);
panic!("No alias output in transaction essence")
_ => panic!("No tx payload"),

Run the Example

Run the example by running the following command:

cargo run --example alias --release

Expected Output

Transaction with new alias output sent:
Transaction with foundry output sent:
Transaction with native tokens burnt sent:
Transaction with native tokens sent:
Second transaction with native tokens sent:
Third transaction with native tokens burned sent: