Skip to main content
During development, you can use the developer endpoints without an API key. For production use, you’ll need an API key to avoid rate limits.
When trading into a prediction market, DFlow lets you trade from any arbitrary input mint directly into a prediction market outcome token. For example, a user can trade from BONK -> outcome token, without first swapping BONK → CASH and then CASH -> outcome token. This removes extra steps and streamlines application logic.

Trading Flow Overview

When you want to trade a spot token for a prediction market outcome token, the /order endpoint orchestrates the entire transaction flow behind the scenes. The endpoint automatically:
  1. Detects whether the target market has been tokenized (initialized)
  2. Executes a two-step swap path: Input Token → Settlement Mint → Outcome Token
  3. Handles market tokenization if the market hasn’t been initialized yet
The /order endpoint accepts any spot token as inputMint and any outcome token mint as outputMint. The endpoint automatically constructs the appropriate transaction path based on the market’s initialization status.
If you start from a wallet’s token balances, filter down to outcome mints before selecting your target outputMint.
const METADATA_API_BASE_URL = "https://dev-prediction-markets-api.dflow.net";

const allMintAddresses = nonZeroBalances.map((token) => token.mint);

const response = await fetch(
  `${METADATA_API_BASE_URL}/api/v1/filter_outcome_mints`,
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ addresses: allMintAddresses }),
  }
);

if (!response.ok) {
  throw new Error("Failed to filter outcome mints");
}

const data = await response.json();
const outcomeMints = data.outcomeMints ?? [];
const outcomeTokens = nonZeroBalances.filter((token) =>
  outcomeMints.includes(token.mint)
);

Transaction Flow

All trades from spot tokens to outcome tokens follow a two-step swap path that route through the settlement mint as an intermediate step:
  1. Input Token → Settlement Mint
  2. Settlement Mint → Outcome Token
The /order endpoint returns a single transaction that executes both swaps.
import { Connection, PublicKey, VersionedTransaction } from "@solana/web3.js";

const API_BASE_URL = "https://dev-quote-api.dflow.net";
const API_KEY = process.env.DFLOW_API_KEY; // Optional for dev

const connection = new Connection("https://api.mainnet-beta.solana.com");
const userPublicKey = new PublicKey("USER_WALLET_ADDRESS_HERE");

const inputMint = "So11111111111111111111111111111111111111112"; // SOL
const outputMint = "OUTCOME_TOKEN_MINT_ADDRESS";
const amount = 1_000_000_000; // 1 SOL (9 decimals)

const queryParams = new URLSearchParams({
  inputMint,
  outputMint,
  amount: amount.toString(),
  userPublicKey: userPublicKey.toBase58(),
});

const headers: HeadersInit = {};
if (API_KEY) {
  headers["x-api-key"] = API_KEY;
}

const orderResponse = await fetch(
  `${API_BASE_URL}/order?${queryParams.toString()}`,
  { headers }
).then((x) => x.json());

const transactionBuffer = Buffer.from(orderResponse.transaction, "base64");
const transaction = VersionedTransaction.deserialize(transactionBuffer);

// sign with the user's keypair in your app
transaction.sign([userKeypair]);
const signature = await connection.sendTransaction(transaction);
const statusResponse = await fetch(
  `${API_BASE_URL}/order-status?signature=${signature}`,
  { headers }
).then((x) => x.json());

console.log(statusResponse.status, statusResponse.fills);

Market Initialization States

The /order endpoint automatically detects whether the target market has been tokenized and handles the transaction accordingly:

Initialized Markets

When a market has already been tokenized, the transaction executes the two-step swap path directly: Input Token → Settlement Mint → Outcome Token

Uninitialized Markets

When a market hasn’t been tokenized yet, the /order endpoint automatically includes market tokenization in the transaction before executing the swap: Market Tokenization + (Input Token → Settlement Mint → Outcome Token) The transaction will:
  1. First perform on-demand market tokenization (creating the YES and NO outcome tokens on-chain)
  2. Then execute the two-step swap: Input Token → Settlement Mint → Outcome Token
On-demand market creation (tokenization) incurs a small fee. This fee is automatically included in the transaction when trading into an uninitialized market.

Checking Market Status

You can check a market’s initialization status using the Prediction Market Metadata API. The isInitialized field in the market response indicates whether the market has been tokenized:
  • isInitialized: true - Market is already tokenized; direct swap path.
  • isInitialized: false - Market needs tokenization; transaction will include initialization.
While you can check this status for informational purposes, the /order endpoint handles the detection automatically, so you don’t need to check beforehand.
const METADATA_API_BASE_URL = "https://dev-prediction-markets-api.dflow.net";
const marketMint = "OUTCOME_TOKEN_MINT_ADDRESS";

const marketResponse = await fetch(
  `${METADATA_API_BASE_URL}/api/v1/market/by-mint/${marketMint}`
).then((x) => x.json());

console.log(marketResponse.isInitialized);

API Routes

Key Takeaways

  • The /order endpoint accepts any spot token as input and any outcome token as output.
  • All trades execute through the settlement mint as an intermediate step: Input Token → Settlement Mint → Outcome Token.
  • For initialized markets, the transaction executes the swap path directly.
  • For uninitialized markets, the transaction includes market tokenization before executing the swap path.
  • On-demand market creation (tokenization) incurs a small fee.
  • The endpoint automatically detects market initialization status and constructs the appropriate transaction.
  • You can check isInitialized via the metadata API.