> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lerian.studio/llms.txt
> Use this file to discover all available pages before exploring further.

# Reverse a transaction (API)

> Create the reversal of an approved transaction, a new transaction in the opposite direction, through the Midaz API.

Undo the effect of an approved transaction. Midaz posts a new transaction in the opposite direction and keeps the original transaction as it is.

## Participants

***

* **Your institution.** Your system. It asks for the reversal.
* **Midaz.** The ledger. It checks the request and posts the reversal.

## Before you start

***

* The transaction to reverse is `APPROVED`. Keep its `id`.

## Diagram

***

![Reverse a transaction](https://mintcdn.com/lerian-49cb71fc/KypD0TIKUCqZ014J/images/en/d2/id-midaz-reverse-a-transaction.svg?fit=max\&auto=format\&n=KypD0TIKUCqZ014J\&q=85\&s=20704c7e8fc66ce7fed3eed36510bfc1)

## Steps through the API

***

| # | From | To | Message | Lerian API | What happens |
| - | - | - | - | - | - |
| 1 | Your institution | Midaz | API call | [`POST /v2/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}/revert`](https://docs.lerian.studio/en/reference/products/midaz/v2/revert-transaction) | Ask for the reversal of the transaction. |
| 2 | Midaz | Midaz | Internal step | | Midaz checks that the transaction is `APPROVED`, has no reversal yet and is not a reversal itself. |
| 3 | Midaz | Midaz | Internal step | | Midaz posts a new transaction that moves the amounts of the original in the opposite direction. Its `parentTransactionId` is the `id` of the original. |
| 4 | Midaz | Your institution | API answer | | Midaz answers `201` with the reversal. |
| 5 | Your institution | Midaz | API call | [`GET /v2/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}`](https://docs.lerian.studio/en/reference/products/midaz/v2/get-transaction) | Read the original transaction. |
| 6 | Midaz | Your institution | API answer | | The original transaction is still `APPROVED`. |

## Rules

***

* You cannot reverse a block or an unblock.
* Each operation that uses an operation route needs a route of type `bidirectional`.
* The reversal charges no new fee. It moves the fees of the original transaction back.

## How you know it finished

***

Read the reversal with [`GET /v2/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}`](https://docs.lerian.studio/en/reference/products/midaz/v2/get-transaction). Its status is `APPROVED`, and its `parentTransactionId` is the `id` of the original transaction.

## What can go wrong

***

| Situation | What you see | What to do |
| - | - | - |
| The transaction already has a reversal | `409` with `0087` | Do not create another reversal. The first one stays in the ledger. |
| The transaction is a reversal itself | `409` with `0088` | To move the amount again, post a new transaction. |
| The transaction is not `APPROVED` | `409` with `0099` | Cancel a `PENDING` transaction instead. |
| An operation route of the transaction is not `bidirectional` | `422` with `0150` | Midaz cannot reverse this transaction. |
| The ledger validates routes, and no operation route of the transaction has a `revert` accounting entry | `422` with `0157` | Add a `revert` entry to the operation routes, then reverse again. |
| The fee-debt record of the transaction is not complete | `409` with `0529` | Midaz moved nothing. Try again later. |
| The answer does not arrive | No answer, or a timeout | Send the same reversal again. A `201` returns the reversal. A `409` with `0087` means that the reversal exists. After a `409` with `0084`, wait, then send the same reversal again. |

## Related

***

* [Set up a ledger and post the first transaction](https://docs.lerian.studio/en/diagrams/lerian-products/midaz-create-and-transact.md)
* [Settle fee debt](https://docs.lerian.studio/en/diagrams/lerian-products/midaz-settle-fee-debt.md)
* [Midaz error list](https://docs.lerian.studio/en/reference/products/midaz/error-list.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.