# Swap API

The FluxBeam Swap API allows developers and users to retrieve swap quotes and pre-built transactions to speed up integration.&#x20;

## Quote

The quote API will return the expected output amount and slippage amount based on the provided input amount & token mints.

*Currently, the API only returns pools on FluxBeam DEX.*

{% openapi src="/files/BTXW41fbdTNJwtnc3yie" path="/v1/quote" method="get" %}
[swagger.json](https://1346299214-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT2Aiu5M1GjdMOI04TEPX%2Fuploads%2FtJlI9ZjUrfH7QfZeH8wj%2Fswagger.json?alt=media\&token=5f2a6e1a-245d-4019-ae4b-3cc068913b76)
{% endopenapi %}

### Example

```javascript
const inMint = "So11111111111111111111111111111111111111112"
const outMint = "FLUXBmPhT3Fd1EDVFdg46YREqHBeNypn1h4EbnTzWERX"
const amount = "1000000000"
const slippageBPS = "50"

const uri = `https://api.fluxbeam.xyz/v1/quote?inputMint=${inMint}&outputMint=${outMint}&amount=${amount}&slippageBps=${slippageBPS}`

const quote = await (await fetch(uri)).json()
```

## Swap Transaction

The Transaction endpoint returns a pre-built transaction that is ready to be signed by the payer wallet based on the quote provided.&#x20;

The swap transaction includes the following instructions:

* Priority Fee Price & Units
* Input & Output associated token account creation
* Wrap & Unwrap of SOL accounts

{% openapi src="/files/BTXW41fbdTNJwtnc3yie" path="/v1/swap/transaction" method="post" %}
[swagger.json](https://1346299214-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT2Aiu5M1GjdMOI04TEPX%2Fuploads%2FtJlI9ZjUrfH7QfZeH8wj%2Fswagger.json?alt=media\&token=5f2a6e1a-245d-4019-ae4b-3cc068913b76)
{% endopenapi %}

### Example

```javascript
import { Connection, Keypair, VersionedTransaction } from '@solana/web3.js';

const uri = `https://api.fluxbeam.xyz/v1/swap/transaction`

const resp = await(await fetch(uri, {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.strinfgify({
        //
    })
})).json()

//Decode the transaction from base64
const swapTransactionBuf = Buffer.from(resp.transaction, 'base64');
const transaction = VersionedTransaction.deserialize(swapTransactionBuf);

console.log("Transaction",transaction);

//Sign the transaction with the payer wallet
transaction.sign([wallet.payer]);

//Execute the transaction
const client= new Connection('{RPC_URL}');
const sig = client.sendRawTransaction(transaction.serialize(), {maxRetries: 3, skipPreflight: true})

console.log("Transaction Signature:", sig)
```

###

### Swap Instruction

The instruction endpoint will return the FluxBeam swap instruction for a given quote. The instruction should be included within a transaction that handles the priority fee & associated token account calls.

{% openapi src="/files/BTXW41fbdTNJwtnc3yie" path="/v1/swap/instruction" method="post" %}
[swagger.json](https://1346299214-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT2Aiu5M1GjdMOI04TEPX%2Fuploads%2FtJlI9ZjUrfH7QfZeH8wj%2Fswagger.json?alt=media\&token=5f2a6e1a-245d-4019-ae4b-3cc068913b76)
{% endopenapi %}

### Example

```javascript
import { Connection, Keypair, Transaction} from '@solana/web3.js';

const uri = `https://api.fluxbeam.xyz/v1/instruction`

const resp = await(await fetch(uri, {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.strinfgify({
        quote, //Response from /quote
        userPublicKey: "{PAYER_WALLET_ADDRESS}",
        priorityFeeLamports: 100,
        wrapAndUnwrapSol: true
    })
})).json()


//Decode the instruction
const ix = new TransactionInstruction({
    programId: new PublicKey(instruction.programId),
    keys: instruction.accounts.map((key) => ({
      pubkey: new PublicKey(key.Pubkey),
      isSigner: key.IsSigner,
      isWritable: key.IsWritable,
    })),
    data: Buffer.from(instruction.data, "base64"),


//Create new transaction
const transaction = new Transaction();

//Add pre-swap instructions
// transaction.add(ataIx)

//Add swap instruction
transaction.add(ix)

//Add post-swap instructions
// transaction.add(wSolClose)

console.log("Transaction",transaction);

//Sign the transaction with the payer wallet
transaction.sign([wallet.payer]);

//Execute the transaction
const client= new Connection('{RPC_URL}');
const sig = client.sendRawTransaction(transaction.serialize(), {maxRetries: 3, skipPreflight: true})

console.log("Transaction Signature:", sig)
```


---

# Agent Instructions: 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:

```
GET https://docs.fluxbeam.xyz/developers/swap-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
