---
title: otxn_id
description: Output the canonical hash of the originating transaction
---
import { Tabs, TabItem } from '@astrojs/starlight/components';

### Behaviour

<Tabs>
<TabItem label="C">
* Write the canonical hash of the originating transaction to the output buffer.
* If flags = 1 and the transaction is an EMIT_FAILURE transaction then write the canonical hash of the originating transaction that caused the emission.
</TabItem>

<TabItem label="JavaScript">
* Output the canonical hash of the originating transaction.
* Returns the transaction hash as an array of numbers, or an ErrorCode if the retrieval fails.
</TabItem>
</Tabs>

### Definition

<Tabs>
<TabItem label="C">
```c
int64_t otxn_id (
    uint32_t write_ptr,
    uint32_t write_len,
    uint32_t flags
);
```
</TabItem>

<TabItem label="JavaScript">
```javascript
function otxn_id(flag: number): ErrorCode | ByteArray
```
</TabItem>
</Tabs>



### Example

<Tabs>
<TabItem label="C">
```c
uint8_t txn_id[32];
int64_t bytes_written = 
    otxn_id(txn_id, 32, 0);
```
</TabItem>

<TabItem label="JavaScript">
```javascript
const tx_id = otxn_id(0)
```
</TabItem>
</Tabs>



### Parameters

<Tabs>
<TabItem label="C">
| Name       | Type      | Description                                                                                                                                                                                                                 |
| ---------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| write_ptr | uint32_t | Pointer to a buffer of to store the hash.                                                                                                                                                                                   |
| write_len | uint32_t | Length of the output buffer. Should be at least 32 bytes.                                                                                                                                                                   |
| flags      | uint32_t | <p>If <code>0</code>:<br />Write the canonical hash of the originating transaction.<br /><br />If <code>1</code> AND the originating transaction is an EMIT_FAILURE:<br />Write the canonical hash of the emitting transaction.</p> |
</TabItem>

<TabItem label="JavaScript">
| Name  | Type   | Description                                                                                                                                                                                                                 |
| ----- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| flags | number | <p>If <code>0</code>:<br />Write the canonical hash of the originating transaction.<br /><br />If <code>1</code> AND the originating transaction is an EMIT_FAILURE:<br />Write the canonical hash of the emitting transaction.</p> |
</TabItem>
</Tabs>



### Return Code

<Tabs>
<TabItem label="C">
| Type     | Description                                                                                                                                                                                                                                                |
| -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| int64_t | <p>The number of bytes written<br /><br />If negative, an error:<br /><code>OUT_OF_BOUNDS</code><br />- pointers/lengths specified outside of hook memory.<br /><br /><code>TOO_SMALL</code><br />- output buffer was not large enough to hold the serialized object</p> |
</TabItem>

<TabItem label="JavaScript">
| Type                   | Description                                                                                  |
| ---------------------- | -------------------------------------------------------------------------------------------- |
| ErrorCode \| ByteArray | Returns the transaction hash as an array of numbers, or an ErrorCode if the retrieval fails. |
</TabItem>
</Tabs>

