Skip to main content
Glama

jupiter_build_swap_transaction

Construct a Solana token swap transaction using Jupiter's API by providing a quote response and user public key. Enable prioritization fees and compute unit price customization.

Instructions

Build a swap transaction on Jupiter

Input Schema

NameRequiredDescriptionDefault
asLegacyTransactionNo
computeUnitPriceMicroLamportsNo
prioritizationFeeLamportsNo
quoteResponseYes
userPublicKeyYes

Input Schema (JSON Schema)

{ "properties": { "asLegacyTransaction": { "type": "boolean" }, "computeUnitPriceMicroLamports": { "type": "number" }, "prioritizationFeeLamports": { "type": "number" }, "quoteResponse": { "type": "string" }, "userPublicKey": { "type": "string" } }, "required": [ "quoteResponse", "userPublicKey" ], "type": "object" }

Implementation Reference

  • Core handler function that implements the jupiter_build_swap_transaction tool. Validates user public key, parses quoteResponse JSON, builds POST request to Jupiter's /swap-instructions endpoint, fetches and returns the swap transaction data or error response.
    export const buildSwapTransactionHandler = async (input: BuildSwapTransactionInput): Promise<ToolResultSchema> => { try { // Validate user public key const userPublicKeyResult = validatePublicKey(input.userPublicKey); if (!(userPublicKeyResult instanceof PublicKey)) { return userPublicKeyResult; } // Parse the quote response let quoteResponse; try { quoteResponse = JSON.parse(input.quoteResponse); } catch (error) { return createErrorResponse(`Invalid quote response: ${error instanceof Error ? error.message : String(error)}`); } // Build the request body const requestBody: any = { quoteResponse, userPublicKey: input.userPublicKey }; if (input.prioritizationFeeLamports !== undefined) { requestBody.prioritizationFeeLamports = input.prioritizationFeeLamports; } if (input.computeUnitPriceMicroLamports !== undefined) { requestBody.computeUnitPriceMicroLamports = input.computeUnitPriceMicroLamports; } if (input.asLegacyTransaction !== undefined) { requestBody.asLegacyTransaction = input.asLegacyTransaction; } // Make the API request const response = await fetch(`${JUPITER_API_BASE_URL}/swap-instructions`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(requestBody) }); if (!response.ok) { const errorText = await response.text(); return createErrorResponse(`Error building swap transaction: ${response.status} ${response.statusText} - ${errorText}`); } const swapData = await response.json(); return createSuccessResponse(`Swap transaction: ${JSON.stringify(swapData, null, 2)}`); } catch (error) { return createErrorResponse(`Error building swap transaction: ${error instanceof Error ? error.message : String(error)}`); } };
  • Input schema definition for the jupiter_build_swap_transaction tool, specifying properties and required fields matching the handler input.
    { name: "jupiter_build_swap_transaction", description: "Build a swap transaction on Jupiter", inputSchema: { type: "object", properties: { quoteResponse: { type: "string" }, userPublicKey: { type: "string" }, prioritizationFeeLamports: { type: "number" }, computeUnitPriceMicroLamports: { type: "number" }, asLegacyTransaction: { type: "boolean" } }, required: ["quoteResponse", "userPublicKey"] } },
  • src/tools.ts:59-63 (registration)
    Registration of the handler function for 'jupiter_build_swap_transaction' in the handlers dictionary, mapping tool name to buildSwapTransactionHandler.
    export const handlers: handlerDictionary = { "jupiter_get_quote": getQuoteHandler, "jupiter_build_swap_transaction": buildSwapTransactionHandler, "jupiter_send_swap_transaction": sendSwapTransactionHandler };
  • TypeScript interface defining the input shape for buildSwapTransactionHandler, used for type safety in the handler.
    export interface BuildSwapTransactionInput { quoteResponse: string; userPublicKey: string; prioritizationFeeLamports?: number; computeUnitPriceMicroLamports?: number; asLegacyTransaction?: boolean; }
  • Helper function validatePublicKey used in the handler to validate the userPublicKey input and return error response if invalid.
    export const validatePublicKey = (publicKeyString: string): PublicKey | ToolResultSchema => { const { publicKey, error } = createPublicKey(publicKeyString); if (error) { return createErrorResponse(error); } return publicKey!; };

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dcSpark/mcp-server-jupiter'

If you have feedback or need assistance with the MCP directory API, please join our Discord server