library / How to use Solana / HS0007
Solana swap API quote
One TypeScript file calls the Carbium quote plane on the RPC endpoint. With no arguments it sends health. Given two mints and an amount it asks for a swap quote and prints the reply and platform fee, with no signature.
What a quote answers
A swap quote answers one question before any money moves: if I send this much of token A, how much of token B comes back, and through which pools? A swap API turns that question into one request. You name the input mint, the output mint and an amount, and you get an answer you can show to a person or hand to the next step.
On Carbium the quote plane lives on the RPC endpoint. It is a JSON-RPC POST to https://services.carbium.sh with the method quote. The same endpoint serves swap, health and prices, four methods and no more. One account key works on this host once it carries the QUOTE scope, and a key without that scope gets -32001 on quote. There is no second host to set up.
A quote is a read. It carries no signature and changes nothing on chain, so the file below needs no keypair and holds no funds. A transaction only appears later, from swap, and only your own software can sign it.
The code
Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24, which strips TypeScript types on its own. With no arguments the file sends health and prints the reply. That confirms the endpoint answers. With an input mint, an output mint and an amount in base units, it sends quote instead. A fourth argument sets slippage in basis points and is 50 when left out.
For example, node main.ts So11111111111111111111111111111111111111112 EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v 100000000 50 asks how much USDC 0.1 SOL buys at 50 basis points of slippage.
// HS0007 · Solana swap API quote// https://carbium.sh/library/howsolana/solana-swap-api-quote// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { setTimeout as sleep } from "node:timers/promises";import { address } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1);}const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";// No arguments: the read-only health method. Two mints and an amount: a quote.const [inArg, outArg, amountArg, slippageArg = "50"] = process.argv.slice(2);let method = "health";let params: unknown[] | undefined;if (inArg !== undefined) { // Base units only: 100000000 lamports is 0.1 SOL. const amount = Number(amountArg); const slippageBps = Number(slippageArg); if (outArg === undefined || !Number.isSafeInteger(amount) || amount <= 0 || !Number.isSafeInteger(slippageBps) || slippageBps < 0) { console.error("Usage: node main.ts [input-mint output-mint amount-in-base-units [slippage-bps]]"); process.exit(1); } // address() rejects a malformed mint before any request leaves this process. method = "quote"; params = [{ src_mint: address(inArg), dst_mint: address(outArg), amount_in: amount, slippage_bps: slippageBps }];}type Reply = { result?: unknown; error?: { code: number; message: string } };const ask = () => fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-api-key": key }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }), });let res = await ask();if (res.status === 429) { const seconds = Number(res.headers.get("retry-after")) || 1; console.error(`429: waiting ${seconds}s, then one more try`); await sleep(seconds * 1000); res = await ask();}// A refused request still carries a JSON-RPC error body: read it before the status.const reply = (await res.json().catch(() => ({}))) as Reply;if (reply.error) { console.error(`${method} failed: ${reply.error.code} ${reply.error.message}`); process.exit(1);}if (!res.ok) { console.error(`HTTP ${res.status}`); process.exit(1);}console.log(JSON.stringify(reply.result, null, 2));const fee = method === "quote" ? (reply.result as Record<string, unknown>)?.platformFee : undefined;if (fee !== undefined) console.log(`platformFee: ${JSON.stringify(fee)}`);
run: node main.ts [input-mint output-mint amount-in-base-units [slippage-bps]]
Four parts of the file carry the job. The key travels in the x-api-key header, and the URL comes from CARBIUM_RPC_URL with https://services.carbium.sh as the fallback, so the same file points at any Carbium endpoint you are given. address() from @solana/kit checks that both mints are well-formed public keys before anything leaves your machine, which turns a typo into a local error. The amount is checked as a safe integer. The request itself is a plain fetch POST, because the typed rpc object in @solana/kit covers the standard Solana methods and quote is not one of them.
The reply is printed whole. For a quote the file also pulls out platformFee and prints it on its own line, because it is a cost the user pays. Every other field stays in the printed JSON for you to read.
Reading the request and reply
A quote request carries four fields. Each one is a decision you make before the call.
| Field | Example value | Meaning |
|---|---|---|
| src_mint | So11111111111111111111111111111111111111112 | The token you give. This address is wrapped SOL, the mint that stands for SOL inside swaps. |
| dst_mint | EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v | The token you want back. This one is USDC. |
| amount_in | 100000000 | An integer in the input token's smallest unit. SOL has 9 decimals, so 100000000 is 0.1 SOL. |
| slippage_bps | 50 | The movement you accept between quote and landing, in basis points. 50 is 0.5%. |
Amounts stay integers in base units from the first argument to the last line. A JavaScript number holds integers exactly up to 2^53, about 9.0 x 10^15, which is about 9 million SOL in lamports and far more USDC than one swap needs. The file refuses an amount past that limit instead of rounding it. Divide by 10 ** decimals only when you show a number to a person.
The reply for the 0.1 SOL request above carries these fields next to platformFee.
| Field | Value in that reply | Meaning |
|---|---|---|
| destAmountOut | 6909272 | What the route returns, in the output token's base units. USDC has 6 decimals, so this is 6.909272 USDC. |
| minAmountOut | 6874725 | The floor your slippage allows. 6874725 is 6909272 less 0.5%, the 50 basis points sent in the request. |
| routePlan | one hop at 100 percent | The pools the amount goes through. This route is a single hop with inAmount 99950000 and outAmount 6909272. |
| quoteId | 1beadcd49ef21578406ee4ae39221a10 | An identifier the reply gives this quote. |
A quote is computed from pool state at one moment. A busy pool can change from one slot to the next, so the state you quoted against may be behind when the transaction lands. Slippage is the bound you set on that movement. A tight value fails more transactions when pools move, and a loose value lets more of the move go against the swapper.
On Carbium
A key needs the QUOTE scope to call quote. Without it the call is refused with HTTP 401 and -32001 "api key is not enabled for quote", which the file prints as code and message. Quote plans are set per account: Free at $0 allows 10 quotes per second, Starter at $7 allows 50, Pro at $28 allows 150 and Elite at $104 allows 500. Limits are per account, so all the keys of one account share one budget.
On an HTTP 429 the file reads the retry-after header, sleeps for that many seconds and tries once more. A second 429 ends the run, and so does any other HTTP error.
-32601 means method not found: a misspelt name, which no retry fixes. The file treats every JSON-RPC error as final and prints its code and message.
A quote screen calls this on every input change, so debounce the input, because each request spends part of the account's per-second budget.
From quote to swap
The next step is the swap method. It returns one unsigned transaction for a quote. Your software signs it and sends it, through Carbium RPC or any other. A quote needs no key material and a swap does, so keep the signer in code you review on its own.
For wallet-side reads around a swap, such as the SOL balance or a token balance before and after, see SPL token balance. For the rate-limit side of a quote screen, see RPC rate-limit alerts.
Where it ends
A quote is an estimate, and no promise comes with it. It describes pool state when it was computed. The transaction that swap returns lands later, and slippage is the only protection against the gap between the two.
The file prints the reply and does not interpret it. It does not check that a route exists or that the output mint has liquidity. Whatever error comes back, the file prints its code and message and exits.
The file makes one call per run. A screen that refreshes quotes needs its own timer and its own backoff. On Free, 10 quotes per second is the ceiling for the whole account, so a few users typing into amount fields can use it up.
The reply says nothing about your wallet. Whether it holds the input amount, whether a token account exists for the output mint and how much SOL it keeps for fees are separate reads.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| HTTP 429 with a retry-after hint. | Wait for the number of seconds in the header and try once more, which the file does. If it repeats, slow the caller down, debounce the input, or move the account to a quote plan with a higher per-second rate. |
| HTTP 401 with -32001 "api key is not enabled for quote". | The key lacks the QUOTE scope. Create or edit a key so it carries the QUOTE scope, and send the whole key. |
| Error -32601 method not found. | Fix the method name. The quote plane has four methods, quote, swap, health and prices, and a misspelling will keep answering -32601, so do not retry it. |
| A JSON-RPC error about the params. | Read the code and message the file prints, then compare the slippage, mints and amount you pass with the quote parameters. The file already rejects a malformed mint and a non-integer amount before any request goes out, so a params error points at a value the quote method does not accept. |
| An output that is off by a power of ten, such as 10^9 for SOL. | The amount went in as whole tokens. It must be in base units, so 0.1 SOL is 100000000, and the output is in the output token's base units too. Divide by 10 ** decimals for display. |
Questions
Do I need a wallet or a signature to get a quote?
No. A quote is a read, so the file holds no keypair and signs nothing. The Carbium key only identifies your account to the quote plane.
What is the platform fee on a quote?
It is 5 bps (0.05%), reported as platformFee. It is an on-chain fee inside the transaction your software signs and sends.
How often can I ask for a quote?
The ceiling is per account: 10 quotes per second on Free, 50 on Starter, 150 on Pro and 500 on Elite. A call over the ceiling gets HTTP 429, and the file waits for the retry-after header and tries once more.
How do I turn the quote into a transaction?
Call the swap method. It returns one unsigned transaction for a quote, and your software signs and sends it through Carbium RPC or any other.