> For the complete documentation index, see [llms.txt](https://docs.tanx.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tanx.fi/tech/api-documentation/internal-transfer/create-internal-transfer.md).

# Create Internal Transfer

To create an internal transfer, first obtain the msg hash from the initiate endpoint  <mark style="background-color:purple;">/initiate/</mark>. Next, sign msgHash with your L2 key using the [`signInternalTxMsgHash`](https://github.com/Brine-Finance-Libs/brine-connector-nodejs/blob/5643daec485b13a118413ebeb6893dd8eb3f075b/src/utils.ts#L62) function. Finally, provide the signed message along with other required information to the process transfer API <mark style="background-color:purple;">/process/</mark>.

> Note: You will need to include the JWT Auth token to request headers to access this endpoint. To get the JWT Auth Token, refer the [login](/tech/api-documentation/account/login.md) section of this document.

## 1. Initiate Internal Transfer <a href="#new-order-nonce" id="new-order-nonce"></a>

Before creating a new Internal Transfer, you will be required to obtain a msg hash by making use of this endpoint. Please note that this is a Private 🔒 route which means it needs to be authorized by the account initiating this request.

#### Endpoint <a href="#endpoint" id="endpoint"></a>

```bash
POST /sapi/v1/internal_transfers/v2/initiate/
```

#### Request Headers <a href="#request-headers" id="request-headers"></a>

```json
{
  "Authorization": "JWT ***"
}
```

#### Request Body <a href="#request-body" id="request-body"></a>

<table><thead><tr><th width="228">Parameter</th><th width="154">Type</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td>organization_key</td><td>string</td><td>Yes</td><td>Reach out to tanX (<a href="mailto:support@tanx.fi">tanx.fi</a>) to get the organization key and API key.</td></tr><tr><td>api_key</td><td>string</td><td>Yes</td><td>Reach out to tanX (<a href="mailto:support@tanx.fi">support@tanx.fi</a>) to get the organization key and API key.</td></tr><tr><td>client_reference_id</td><td>string</td><td>No</td><td>This is an optional field. If not specified, then it’s generated randomly. You can use this to uniquely identify a transfer at your end.</td></tr><tr><td>currency</td><td>string</td><td>Yes</td><td>The currency of the transfer (e.g., USDC). Currently, we support USDC.</td></tr><tr><td>amount</td><td>decimal</td><td>Yes</td><td>The amount of the transfer</td></tr><tr><td>destination_address</td><td>string</td><td>Yes</td><td>The receiver's eth address.</td></tr></tbody></table>

#### Response

```json
{
  "status": "success",
  "message": "Please sign the message, to complete the transaction",
  "payload": {
    "msg_hash": "0x1234567890abcdef",
    "nonce": 123456
  }
}
```

## 2. Sign the msg\_hash with the L2 key pair

To sign the `msg_hash` obtained from the above endpoint, you need to generate the L2 key pair. Use the [Nodejs-sdk](https://github.com/Brine-Finance-Libs/brine-connector-nodejs/tree/main)  to generate the L2 key pair from your Ethereum private key.\
\
\
**2.1 Generating a L2 key pair from an ethereum private key**

```javascript
import { generateKeyPairFromEthPrivateKey } from '@tanx-libs/tanx-connector'

const keypair = generateKeyPairFromEthPrivateKey(
    ethPrivateKey, 
    'testnet' // The default value is "mainnet," and the allowed values are ['testnet', 'mainnet'].
) 
```

\
**2.2 Use the L2 key pair to sign the `msg_hash` obtained from the initiate endpoint.**

```javascript
import { signInternalTxMsgHash } from '@tanx-libs/tanx-connector'

 const signature = signInternalTxMsgHash(
      keyPair,
      initiateResponse.payload.msg_hash,
 )
```

## 3. Process Internal Transfer <a href="#new-order-nonce" id="new-order-nonce"></a>

Process an internal transfer between two users. Before processing the transfer, you will be required to obtain a signature by using the initiate endpoint and the signing utils. Please note that this is a private 🔒 route, which means it needs to be authorized by the account initiating this request.<br>

**Endpoint**

```bash
POST /sapi/v1/internal_transfers/v2/process/
```

#### Request Headers <a href="#request-headers-1" id="request-headers-1"></a>

```json
{
  "Authorization": "JWT ***"
}
```

#### Request Body <a href="#request-body-1" id="request-body-1"></a>

| Parameter         | Type                              | Required | Description                                                                        |
| ----------------- | --------------------------------- | -------- | ---------------------------------------------------------------------------------- |
| organization\_key | string                            | Yes      | Reach out to tanX (<support@tanx.fi>) to get the organization key and API key.     |
| api\_key          | string                            | Yes      | Reach out to tanX (<support@tanx.fi>) to get the organization key and API key.     |
| signature         | JSON object with “r” and “s” keys | Yes      | The signature of the transfer (obtained from the above sign endpoint)              |
| nonce             | integer                           | Yes      | The nonce of the transfer (obtained from the above signInternalTxMsgHash function) |
| msg\_hash         | string                            | Yes      | The message hash of the transfer (obtained from the above Initiate endpoint)       |

**Response**

```json
{
    "status": "success",
    "message": "Internal transfer processed successfully",
    "payload": {
        "client_reference_id": "795278363509343",
        "amount": "1",
        "currency": "usdc",
        "from_address": "0x1234",
        "destination_address": "0x1234",
        "status": "success",
        "created_at": "2023-07-12T04:42:22.639933Z",
        "updated_at": "2023-07-12T04:43:37.373071Z"
    }
}
```
