0

I am following along the Jupiter v6 docs to implement the Swap API. I am successfully getting the Quote Response from the v6/quote endpoint. After that I am trying to get the serialized transactions from the v6/swap endpoint like this:

   interface SwapParams {
    userPublicKey: String,
    quoteResponse: Promise<any>,
    wrapAndUnwrapSol?: Boolean,
    useSharedAccounts?: Boolean,
    feeAccount?: String,
    computeUnitPriceMicroLamports?: Number,
    asLegacyTransaction?: Boolean,
    useTokenLedger?: Boolean,
    destinationTokenAccount?: String
}
   const performSwap = async (params: SwapParams) => {
    const response = await axios.post('https://quote-api.jup.ag/v6/swap', { params }, {
        headers: {
            'Content-Type': 'application/json'
        }
    })
    const { swapTransaction } = response.data
    return swapTransaction
}

And then calling the above function like the following

const swapParameters: SwapParams = {
        userPublicKey: 'Fy6hRQPneFmrJ9QuY7PSvDJjWvVpeuoMcss5yocyezfy',
        quoteResponse: res, // res is response from `v6/swap` endpoint
        feeAccount: 'G1CHEdRynmCWDx2GZcZmrzEFGG9RGKSUVCzvnwuj6L2d'
    }
const swapRes = await performSwap(swapParameters)

However this is outputting a big 400 error in the console

        reject(new AxiosError(
           ^
AxiosError: Request failed with status code 400
    at settle (C:\Users\Anish Mukherjee\Desktop\work\Randy\KittySniperBot\node_modules\axios\lib\core\settle.js:19:12)
    at IncomingMessage.handleStreamEnd (C:\Users\Anish Mukherjee\Desktop\work\Randy\KittySniperBot\node_modules\axios\lib\adapters\http.js:589:11)
    at IncomingMessage.emit (node:events:526:35)
    at IncomingMessage.emit (node:domain:489:12)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
    at Axios.request (C:\Users\Anish Mukherjee\Desktop\work\Randy\KittySniperBot\node_modules\axios\lib\core\Axios.js:45:41)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_BAD_REQUEST',
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [ 'xhr', 'http' ],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function], Blob: [class Blob] },
    validateStatus: [Function: validateStatus],
    headers: Object [AxiosHeaders] {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/json',
      'User-Agent': 'axios/1.6.8',
      'Content-Length': '865',
      'Accept-Encoding': 'gzip, compress, deflate, br'
    },
    method: 'post',
    url: 'https://quote-api.jup.ag/v6/swap',
    data: '{"params":{"userPublicKey":"Fy6hRQPneFmrJ9QuY7PSvDJjWvVpeuoMcss5yocyezfy","quoteResponse":{"inputMint":"So11111111111111111111111111111111111111112","inAmount":"100000000","outputMint":"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v","outAmount":"17086946","otherAmountThreshold":"17001512","swapMode":"ExactIn","slippageBps":50,"platformFee":{"amount":"172595","feeBps":100},"priceImpactPct":"0","routePlan":[{"swapInfo":{"ammKey":"EXHyQxMSttcvLPwjENnXCPZ8GmLjJYHtNBnAkcFeFKMn","label":"Raydium CLMM","inputMint":"So11111111111111111111111111111111111111112","outputMint":"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v","inAmount":"100000000","outAmount":"17259541","feeAmount":"8400","feeMint":"So11111111111111111111111111111111111111112"},"percent":100}],"contextSlot":259704157,"timeTaken":0.00727495},"feeAccount":"G1CHEdRynmCWDx2GZcZmrzEFGG9RGKSUVCzvnwuj6L2d"}}'
  },
  request: <ref *1> ClientRequest {
    _events: [Object: null prototype] {
      abort: [Function (anonymous)],
      aborted: [Function (anonymous)],
      connect: [Function (anonymous)],
      error: [Function (anonymous)],
      socket: [Function (anonymous)],
      timeout: [Function (anonymous)],
      finish: [Function: requestOnFinish]
    },
    _eventsCount: 7,
    _maxListeners: undefined,
    outputData: [],
    outputSize: 0,
    writable: true,
    destroyed: false,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    maxRequestsOnConnectionReached: false,
    _defaultKeepAlive: true,
    useChunkedEncodingByDefault: true,
    sendDate: false,
    _removedConnection: false,
    _removedContLen: false,
    _removedTE: false,
    strictContentLength: false,
    _contentLength: '865',
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    _closed: false,
    socket: TLSSocket {
      _tlsOptions: [Object],
      _secureEstablished: true,
      _securePending: false,
      _newSessionPending: false,
      _controlReleased: true,
      secureConnecting: false,
      _SNICallback: null,
      servername: 'quote-api.jup.ag',
      alpnProtocol: false,
      authorized: true,
      authorizationError: null,
      encrypted: true,
      _events: [Object: null prototype],
      _eventsCount: 10,
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'quote-api.jup.ag',
      _closeAfterHandlingError: false,
      _readableState: [ReadableState],
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: undefined,
      _server: null,
      ssl: [TLSWrap],
      _requestCert: true,
      _rejectUnauthorized: true,
      parser: null,
      _httpMessage: [Circular *1],
      [Symbol(res)]: [TLSWrap],
      [Symbol(verified)]: true,
      [Symbol(pendingSession)]: null,
      [Symbol(async_id_symbol)]: 60,
      [Symbol(kHandle)]: [TLSWrap],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kSetNoDelay)]: false,
      [Symbol(kSetKeepAlive)]: true,
      [Symbol(kSetKeepAliveInitialDelay)]: 60,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(connect-options)]: [Object]
    },
    _header: 'POST /v6/swap HTTP/1.1\r\n' +
      'Accept: application/json, text/plain, */*\r\n' +
      'Content-Type: application/json\r\n' +
      'User-Agent: axios/1.6.8\r\n' +
      'Content-Length: 865\r\n' +
      'Accept-Encoding: gzip, compress, deflate, br\r\n' +
      'Host: quote-api.jup.ag\r\n' +
      'Connection: close\r\n' +
      '\r\n',
    _keepAliveTimeout: 0,
    _onPendingData: [Function: nop],
    agent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object: null prototype],
      requests: [Object: null prototype] {},
      sockets: [Object: null prototype],
      freeSockets: [Object: null prototype] {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      scheduling: 'lifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 1,
      maxCachedSessions: 100,
      _sessionCache: [Object],
      [Symbol(kCapture)]: false
    },
    socketPath: undefined,
    method: 'POST',
    maxHeaderSize: undefined,
    insecureHTTPParser: undefined,
    joinDuplicateHeaders: undefined,
    path: '/v6/swap',
    _ended: true,
    res: IncomingMessage {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      socket: [TLSSocket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      rawHeaders: [Array],
      rawTrailers: [],
      joinDuplicateHeaders: undefined,
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 400,
      statusMessage: 'Bad Request',
      client: [TLSSocket],
      _consuming: false,
      _dumped: false,
      req: [Circular *1],
      responseUrl: 'https://quote-api.jup.ag/v6/swap',
      redirects: [],
      [Symbol(kCapture)]: false,
      [Symbol(kHeaders)]: [Object],
      [Symbol(kHeadersCount)]: 28,
      [Symbol(kTrailers)]: null,
      [Symbol(kTrailersCount)]: 0
    },
    aborted: false,
    timeoutCb: null,
    upgradeOrConnect: false,
    parser: null,
    maxHeadersCount: null,
    reusedSocket: false,
    host: 'quote-api.jup.ag',
    protocol: 'https:',
    _redirectable: Writable {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _options: [Object],
      _ended: true,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 865,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [Circular *1],
      _currentUrl: 'https://quote-api.jup.ag/v6/swap',
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false,
    [Symbol(kBytesWritten)]: 0,
    [Symbol(kNeedDrain)]: false,
    [Symbol(corked)]: 0,
    [Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],
      'content-type': [Array],
      'user-agent': [Array],
      'content-length': [Array],
      'accept-encoding': [Array],
      host: [Array]
    },
    [Symbol(errored)]: null,
    [Symbol(kHighWaterMark)]: 16384,
    [Symbol(kRejectNonStandardBodyWrites)]: false,
    [Symbol(kUniqueHeaders)]: null
  },
  response: {
    status: 400,
    statusText: 'Bad Request',
    headers: Object [AxiosHeaders] {
      date: 'Fri, 12 Apr 2024 11:07:36 GMT',
      'content-type': 'application/json',
      'content-length': '62',
      connection: 'close',
      'cf-ray': '8732c0f1aab5859c-BOM',
      'cf-cache-status': 'DYNAMIC',
      'access-control-allow-origin': '*',
      'access-control-allow-headers': '*',
      'access-control-max-age': '86400',
      'access-control-request-method': 'GET, POST, OPTIONS',
      m: '',
      vary: 'Accept-Encoding',
      server: 'cloudflare',
      'alt-svc': 'h3=":443"; ma=86400'
    },
    config: {
      transitional: [Object],
      adapter: [Array],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      env: [Object],
      validateStatus: [Function: validateStatus],
      headers: [Object [AxiosHeaders]],
      method: 'post',
      url: 'https://quote-api.jup.ag/v6/swap',
      data: '{"params":{"userPublicKey":"Fy6hRQPneFmrJ9QuY7PSvDJjWvVpeuoMcss5yocyezfy","quoteResponse":{"inputMint":"So11111111111111111111111111111111111111112","inAmount":"100000000","outputMint":"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v","outAmount":"17086946","otherAmountThreshold":"17001512","swapMode":"ExactIn","slippageBps":50,"platformFee":{"amount":"172595","feeBps":100},"priceImpactPct":"0","routePlan":[{"swapInfo":{"ammKey":"EXHyQxMSttcvLPwjENnXCPZ8GmLjJYHtNBnAkcFeFKMn","label":"Raydium CLMM","inputMint":"So11111111111111111111111111111111111111112","outputMint":"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v","inAmount":"100000000","outAmount":"17259541","feeAmount":"8400","feeMint":"So11111111111111111111111111111111111111112"},"percent":100}],"contextSlot":259704157,"timeTaken":0.00727495},"feeAccount":"G1CHEdRynmCWDx2GZcZmrzEFGG9RGKSUVCzvnwuj6L2d"}}'
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      strictContentLength: false,
      _contentLength: '865',
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      socket: [TLSSocket],
      _header: 'POST /v6/swap HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: application/json\r\n' +
        'User-Agent: axios/1.6.8\r\n' +
        'Content-Length: 865\r\n' +
        'Accept-Encoding: gzip, compress, deflate, br\r\n' +
        'Host: quote-api.jup.ag\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      joinDuplicateHeaders: undefined,
      path: '/v6/swap',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'quote-api.jup.ag',
      protocol: 'https:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype],
      [Symbol(errored)]: null,
      [Symbol(kHighWaterMark)]: 16384,
      [Symbol(kRejectNonStandardBodyWrites)]: false,
      [Symbol(kUniqueHeaders)]: null
    },
    data: { error: 'missing field `userPublicKey` at line 1 column 865' }
  }
}

From what I understand, the error message is saying that I have not passed in the userPublicKey, but I have passed it, along with the fee account and the Quote response object as well, so really unsure how to proceed from here. Any help is appreciated!

1 Answer 1

0

It was a simple case of wrong formatting. When I am sending the POST request using axios, I am sending the parameters object params enclosed in double braces, which is what was throwing the error. Removing the double braces fixed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.