> For the complete documentation index, see [llms.txt](https://docs.heyhal.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.heyhal.xyz/api-documentation/mint-token.md).

# Mint Token

**Endpoint:** <mark style="color:yellow;">`POST /api/v1/coin/mint`</mark>

### Description

This endpoint creates and returns a serialized transaction for minting new tokens, setting up metadata, creating liquidity pools, and executing initial swaps. The transaction includes token creation, metadata initialization, and initial liquidity setup.

### Authentication

Required: Yes

Type: Bearer Token (JWT)

### Request

#### Headers

#### Content-Type: application/json

#### Authorization: Bearer \<your\_jwt\_token>

#### Body Parameters

| Parameter | Type   | Required | Description                                   |
| --------- | ------ | -------- | --------------------------------------------- |
| token     | string | Yes      | The mint address of the token                 |
| amount    | string | Yes      | The amount to swap in the initial transaction |

Example Request:

{

&#x20; "token": "TokenMintAddress123...",

&#x20; "amount": "1000000000"

}

### Response

#### Success Response

Status Code: 200 OK

Content-Type: application/json

interface MintResponse {

&#x20; serializedTransaction: string;  // base58 encoded serialized transaction

}

#### Error Responses

**Token Not Found**

Status Code: 404

"token not found"

**Server Error**

Status Code: 500

{

&#x20; "error": "An internal error occurred. Please try again later."

}

### Example Usage

### import { Connection, PublicKey, Transaction } from "@solana/web3.js";

### import bs58 from "bs58";

### async function mintToken(

### &#x20; tokenMintAddress: string,&#x20;

### &#x20; amount: string,

### &#x20; wallet: any // Wallet adapter

### ) {

### &#x20; try {

### &#x20;   const response = await fetch('<https://api.heyhal.xyz/api/v1/coin/mint>', {

### &#x20;     method: 'POST',

### &#x20;     headers: {

### &#x20;       'Content-Type': 'application/json',

### &#x20;       'Authorization': \`Bearer ${localStorage.getItem('auth\_token')}\`

### &#x20;     },

### &#x20;     body: JSON.stringify({

### &#x20;       token: tokenMintAddress,

### &#x20;       amount: amount

### &#x20;     })

### &#x20;   });

### &#x20;   if (!response.ok) {

### &#x20;     throw new Error(\`HTTP error! status: ${response.status}\`);

### &#x20;   }

### &#x20;   const { serializedTransaction } = await response.json();

### &#x20;   // Deserialize and sign the transaction

### &#x20;   const transaction = Transaction.from(

### &#x20;     bs58.decode(serializedTransaction)

### &#x20;   );

### &#x20;   // Sign and send the transaction

### &#x20;   const signature = await wallet.sendTransaction(

### &#x20;     transaction,

### &#x20;     connection

### &#x20;   );

### &#x20;   console.log('Mint transaction sent:', signature);

### &#x20;   return signature;

### &#x20; } catch (error) {

### &#x20;   console.error('Error minting token:', error);

### &#x20;   throw error;

### &#x20; }

### }

### Transaction Details

The created transaction includes multiple instructions:

1\. Create Token Account

Creates the token mint account

Allocates space for mint data

Pays for rent exemption

Initialize Mint

Sets decimals (fixed at 9)

Sets mint and freeze authorities

Initializes supply

Create Associated Token Account

Creates the token account for the minter

Links it to the mint

Mint Tokens

Mints the initial supply

Amount is set to config.constants.tokenSupply \* 10^9

Create Metadata

Creates on-chain metadata

Sets name, symbol, and URI

Makes metadata immutable

Revoke Authorities

Removes mint authority

Removes freeze authority

Create Liquidity Pool

Sets up initial liquidity pool

Adds initial liquidity

Execute Initial Swap

Performs initial token swap

Sets initial price

### Implementation Notes

### // Example of constructing a mint transaction manually

### async function constructMintTransaction(

### &#x20; mint: PublicKey,

### &#x20; payer: PublicKey,

### &#x20; metadata: {

### &#x20;   name: string;

### &#x20;   symbol: string;

### &#x20;   uri: string;

### &#x20; }

### ) {

### &#x20; const transaction = new Transaction();

### &#x20;&#x20;

### &#x20; // 1. Create mint account

### &#x20; const lamports = await getMinimumBalanceForRentExemptMint(connection);

### &#x20; transaction.add(

### &#x20;   SystemProgram.createAccount({

### &#x20;     fromPubkey: payer,

### &#x20;     newAccountPubkey: mint,

### &#x20;     space: MINT\_SIZE,

### &#x20;     lamports,

### &#x20;     programId: TOKEN\_PROGRAM\_ID,

### &#x20;   })

### &#x20; );

### &#x20; // 2. Initialize mint

### &#x20; transaction.add(

### &#x20;   createInitializeMintInstruction(

### &#x20;     mint,

### &#x20;     9, // decimals

### &#x20;     payer,

### &#x20;     payer,

### &#x20;     TOKEN\_PROGRAM\_ID

### &#x20;   )

### &#x20; );

### &#x20; // ... Add remaining instructions

### &#x20;&#x20;

### &#x20; return transaction;

### }

### Best Practices

1\. Transaction Handling

async function handleMintTransaction(

&#x20; tokenMintAddress: string,

&#x20; amount: string

) {

&#x20; try {

&#x20;   // 1. Get the transaction

&#x20;   const { serializedTransaction } = await getMintTransaction(

&#x20;     tokenMintAddress,

&#x20;     amount

&#x20;   );

&#x20;   // 2. Add retry logic for network issues

&#x20;   const MAX\_RETRIES = 3;

&#x20;   let retries = 0;

&#x20;  &#x20;

&#x20;   while (retries < MAX\_RETRIES) {

&#x20;     try {

&#x20;       const signature = await sendAndConfirmTransaction(

&#x20;         serializedTransaction

&#x20;       );

&#x20;       return signature;

&#x20;     } catch (error) {

&#x20;       retries++;

&#x20;       if (retries === MAX\_RETRIES) throw error;

&#x20;       await new Promise(resolve => setTimeout(resolve, 1000 \* retries));

&#x20;     }

&#x20;   }

&#x20; } catch (error) {

&#x20;   console.error('Mint transaction failed:', error);

&#x20;   throw error;

&#x20; }

}

Error Handling

function validateMintParams(

&#x20; tokenMintAddress: string,

&#x20; amount: string

) {

&#x20; if (!tokenMintAddress || !amount) {

&#x20;   throw new Error('Missing required parameters');

&#x20; }

&#x20; const amountNum = Number(amount);

&#x20; if (isNaN(amountNum) || amountNum <= 0) {

&#x20;   throw new Error('Invalid amount');

&#x20; }

&#x20; try {

&#x20;   new PublicKey(tokenMintAddress);

&#x20; } catch {

&#x20;   throw new Error('Invalid token mint address');

&#x20; }

}

Transaction Monitoring

async function monitorMintTransaction(signature: string) {

&#x20; const MAX\_ATTEMPTS = 60; // 1 minute with 1s intervals

&#x20; let attempts = 0;

&#x20; while (attempts < MAX\_ATTEMPTS) {

&#x20;   const status = await connection.getSignatureStatus(signature);

&#x20;  &#x20;

&#x20;   if (status.value?.confirmationStatus === 'confirmed') {

&#x20;     return true;

&#x20;   }

&#x20;  &#x20;

&#x20;   await new Promise(resolve => setTimeout(resolve, 1000));

&#x20;   attempts++;

&#x20; }

&#x20;&#x20;

&#x20; throw new Error('Transaction confirmation timeout');

}

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.heyhal.xyz/api-documentation/mint-token.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
