Get Outputs
You can get outputs by their output ID using the Client.get_output(output_id)
function.
The following code example will:
- Create a
Client
which will connect to the Shimmer Testnet. - Get an output by its output ID.
Code Example
- Rust
- Nodejs
- Python
- Java
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! Calls `GET /api/core/v2/outputs/{outputId}`.
//! Find an output, as JSON, by its identifier.
//! Run: `cargo run --example node_api_core_get_output --release -- [NODE URL] [OUTPUT ID]`.
use std::str::FromStr;
use iota_client::{block::output::OutputId, 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()?;
// Take the output ID from command line argument or use a default one.
let output_id =
OutputId::from_str(&std::env::args().nth(2).unwrap_or_else(|| {
String::from("0xb66fd384cb5755668f1890ea2e41d699db9cf32f3bc422ad3c24ffeb9c7f01d00000")
}))?;
// Get the output.
let output = client.get_output(&output_id).await?;
println!("{output:#?}");
Ok(())
}
Run the Example
Run the example by running the following command:
cargo run --example node_api_core_get_output --release -- https://api.testnet.shimmer.network 0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c
- You can replace
https://api.testnet.shimmer.network
with any node url. - You can replace
0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c
with any other OutputID.
Dotenv
This example uses dotenv, which is not safe to use in production environments.
// Copyright 2021-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Client, initLogger } from '@iota/client';
require('dotenv').config({ path: '../.env' });
// Run with command:
// node ./dist/04_get_output.js
// In this example we will get output from a known outputId
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}
const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
localPow: true,
});
try {
const output = await client.getOutput(
'0xa0b9ad3f5aa2bfcaed30cde6e1d572e93b7e8bb5a417f5a7ef3502889b5dbcb40000',
);
console.log('Output: ', output);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());
You can run the example by running the following command from the bindings/node/examples/
folder:
node dist/04_get_output.js
from iota_client import IotaClient
# Create an IotaClient instance
client = IotaClient({'nodes': ['https://api.testnet.shimmer.network']})
# Get an outputs by its id
output = client.get_output('0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c0000')
print(f'{output}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 04_get_output.py
import org.iota.Client;
import org.iota.types.ClientConfig;
import org.iota.types.expections.ClientException;
import org.iota.types.Output;
import org.iota.types.OutputMetadata;
import org.iota.types.expections.InitializeClientException;
import org.iota.types.ids.OutputId;
import java.util.Map;
public class GetOutputs {
public static void main(String[] args) throws ClientException, InitializeClientException {
// Build the client.
Client client = new Client(new ClientConfig().withNodes(new String[]{"https://api.testnet.shimmer.network"}));
// Set up a output ID for this example.
OutputId outputId = ExampleUtils.setUpOutputId(client);
// Get the output for the given output id.
Map.Entry<Output, OutputMetadata> outputData = client.getOutput(outputId);
// Print the output and its metadata.
System.out.println(outputData.getKey());
System.out.println(outputData.getValue());
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
OutputResponse {
metadata: OutputMetadataResponse {
block_id: "0xa9f11aba1e9965173dc21a47ec4fbfe5b953a6e60277857a3f7a5c1499e7c454",
transaction_id: "0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c",
output_index: 0,
is_spent: true,
milestone_index_spent: Some(
249147,
),
milestone_timestamp_spent: Some(
1659379781,
),
transaction_id_spent: Some(
"0xa927ea21d3c4a49d5c73169c80302ddb223894c182a4b7cfef0af89568262749",
),
milestone_index_booked: 249145,
milestone_timestamp_booked: 1659379771,
ledger_index: 918846,
},
output: Alias(
AliasOutputDto {
kind: 4,
amount: "2000000",
native_tokens: [],
alias_id: AliasIdDto(
"0x0000000000000000000000000000000000000000000000000000000000000000",
),
state_index: 0,
state_metadata: "",
foundry_counter: 0,
unlock_conditions: [
StateControllerAddress(
StateControllerAddressUnlockConditionDto {
kind: 4,
address: Ed25519(
Ed25519AddressDto {
kind: 0,
pub_key_hash: "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3",
},
),
},
),
GovernorAddress(
GovernorAddressUnlockConditionDto {
kind: 5,
address: Ed25519(
Ed25519AddressDto {
kind: 0,
pub_key_hash: "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3",
},
),
},
),
],
features: [
Sender(
SenderFeatureDto {
kind: 0,
address: Ed25519(
Ed25519AddressDto {
kind: 0,
pub_key_hash: "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3",
},
),
},
),
Metadata(
MetadataFeatureDto {
kind: 2,
data: "0x010203",
},
),
],
immutable_features: [
Issuer(
IssuerFeatureDto {
kind: 1,
address: Ed25519(
Ed25519AddressDto {
kind: 0,
pub_key_hash: "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3",
},
),
},
),
],
},
),
}
{
'metadata': {
'blockId': '0xa9f11aba1e9965173dc21a47ec4fbfe5b953a6e60277857a3f7a5c1499e7c454',
'transactionId': '0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c',
'outputIndex': 0,
'isSpent': True,
'milestoneIndexSpent': 249147,
'milestoneTimestampSpent': 1659379781,
'transactionIdSpent': '0xa927ea21d3c4a49d5c73169c80302ddb223894c182a4b7cfef0af89568262749',
'milestoneIndexBooked': 249145,
'milestoneTimestampBooked': 1659379771,
'ledgerIndex': 749737
},
'output': {
'type': 4,
'amount': '2000000',
'aliasId': '0x0000000000000000000000000000000000000000000000000000000000000000',
'stateIndex': 0,
'foundryCounter': 0,
'unlockConditions': [
{
'type': 4,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
},
{
'type': 5,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
}
],
'features': [
{
'type': 0,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
},
{
'type': 2,
'data': '0x010203'
}
],
'immutableFeatures': [
{
'type': 1,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
}
]
}
}
{
'metadata': {
'blockId': '0xa9f11aba1e9965173dc21a47ec4fbfe5b953a6e60277857a3f7a5c1499e7c454',
'transactionId': '0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c',
'outputIndex': 0,
'isSpent': True,
'milestoneIndexSpent': 249147,
'milestoneTimestampSpent': 1659379781,
'transactionIdSpent': '0xa927ea21d3c4a49d5c73169c80302ddb223894c182a4b7cfef0af89568262749',
'milestoneIndexBooked': 249145,
'milestoneTimestampBooked': 1659379771,
'ledgerIndex': 749737
},
'output': {
'type': 4,
'amount': '2000000',
'aliasId': '0x0000000000000000000000000000000000000000000000000000000000000000',
'stateIndex': 0,
'foundryCounter': 0,
'unlockConditions': [
{
'type': 4,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
},
{
'type': 5,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
}
],
'features': [
{
'type': 0,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
},
{
'type': 2,
'data': '0x010203'
}
],
'immutableFeatures': [
{
'type': 1,
'address': {
'type': 0,
'pubKeyHash': '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3'
}
}
]
}
}
{
"type": 3,
"amount": "1000000000",
"unlockConditions": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0xf11b9d362a332263e7773350b0214b742ccffd364d678626bd15556514617058"
}
}
]
}{
"blockId": "0xb6e74f8a623756b26d6146ef34612890b6c858088482620eddaaf4ecb9c8b9fb",
"transactionId": "0xc66155c2bbbd8f22cb55940642aa65a8c5cd07fb1f93c7c4ff53414d45daa577",
"outputIndex": 0,
"isSpent": false,
"milestoneIndexBooked": 949,
"milestoneTimestampBooked": 1658138754,
"ledgerIndex": 749811
}