Create trade v2 swap transaction
curl --request POST \
--url https://public-api-v2.bags.fm/api/v1/trade/v2/swap \
--header 'Content-Type: application/json' \
--header 'x-api-key: <api-key>' \
--data '
{
"requestId": "<string>",
"userPublicKey": "<string>"
}
'import requests
url = "https://public-api-v2.bags.fm/api/v1/trade/v2/swap"
payload = {
"requestId": "<string>",
"userPublicKey": "<string>"
}
headers = {
"x-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({requestId: '<string>', userPublicKey: '<string>'})
};
fetch('https://public-api-v2.bags.fm/api/v1/trade/v2/swap', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://public-api-v2.bags.fm/api/v1/trade/v2/swap",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'requestId' => '<string>',
'userPublicKey' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://public-api-v2.bags.fm/api/v1/trade/v2/swap"
payload := strings.NewReader("{\n \"requestId\": \"<string>\",\n \"userPublicKey\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://public-api-v2.bags.fm/api/v1/trade/v2/swap")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"requestId\": \"<string>\",\n \"userPublicKey\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://public-api-v2.bags.fm/api/v1/trade/v2/swap")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"requestId\": \"<string>\",\n \"userPublicKey\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"success": true,
"response": {
"swapTransaction": "<string>",
"computeUnitLimit": 123,
"prioritizationFeeLamports": 123,
"lastValidBlockHeight": 123,
"feeMint": "<string>",
"feeSide": "input",
"feeLamports": "<string>",
"revenueLamports": "<string>",
"provider": "jupiter"
}
}{
"success": false,
"error": "<string>"
}{
"success": false,
"error": "<string>"
}{
"success": false,
"error": "<string>"
}{
"success": false,
"error": "<string>"
}Trade
Create Trade V2 Swap Transaction
Build the unsigned swap transaction for a previously fetched trade/v2/quote. Loads the cached quote by requestId, enforces that userPublicKey matches the quote’s taker, and assembles a v0 transaction containing the provider instructions, the Bags treasury fee transfer, an optional tracking transfer, and address lookup tables.
The fee transfer is a native SOL transfer when feeMint is SOL, otherwise an idempotent treasury token account creation plus a checked SPL transfer against the mint’s own token program (Token or Token-2022). The taker pays a small amount of rent the first time a given fee mint is collected; the idempotent instruction is a no-op afterward.
POST
/
trade
/
v2
/
swap
Create trade v2 swap transaction
curl --request POST \
--url https://public-api-v2.bags.fm/api/v1/trade/v2/swap \
--header 'Content-Type: application/json' \
--header 'x-api-key: <api-key>' \
--data '
{
"requestId": "<string>",
"userPublicKey": "<string>"
}
'import requests
url = "https://public-api-v2.bags.fm/api/v1/trade/v2/swap"
payload = {
"requestId": "<string>",
"userPublicKey": "<string>"
}
headers = {
"x-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({requestId: '<string>', userPublicKey: '<string>'})
};
fetch('https://public-api-v2.bags.fm/api/v1/trade/v2/swap', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://public-api-v2.bags.fm/api/v1/trade/v2/swap",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'requestId' => '<string>',
'userPublicKey' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://public-api-v2.bags.fm/api/v1/trade/v2/swap"
payload := strings.NewReader("{\n \"requestId\": \"<string>\",\n \"userPublicKey\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://public-api-v2.bags.fm/api/v1/trade/v2/swap")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"requestId\": \"<string>\",\n \"userPublicKey\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://public-api-v2.bags.fm/api/v1/trade/v2/swap")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"requestId\": \"<string>\",\n \"userPublicKey\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"success": true,
"response": {
"swapTransaction": "<string>",
"computeUnitLimit": 123,
"prioritizationFeeLamports": 123,
"lastValidBlockHeight": 123,
"feeMint": "<string>",
"feeSide": "input",
"feeLamports": "<string>",
"revenueLamports": "<string>",
"provider": "jupiter"
}
}{
"success": false,
"error": "<string>"
}{
"success": false,
"error": "<string>"
}{
"success": false,
"error": "<string>"
}{
"success": false,
"error": "<string>"
}Authorizations
API key authentication. Provide your API key as the header value.
Body
application/json
