> ## Documentation Index
> Fetch the complete documentation index at: https://pond.dflow.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Monitor Market Lifecycle

> How to monitor market status changes and redemption availability

<Info>
  During development, you can use the [developer endpoints](/get-started/endpoints)
  without an API key. For production use, [request an API key](/get-started/api-key)
  for higher rate limits.
</Info>

## Market Lifecycle Diagram

Use this lifecycle to decide when to trade and when to redeem. A market can move
forward through the sequence and can temporarily pause in `inactive`.

```mermaid theme={null}
flowchart LR
  initialized --> active
  active --> inactive
  inactive --> active
  inactive --> closed
  active --> closed
  closed --> determined
  determined --> finalized
```

<Steps>
  <Step title="Set up">
    Imports, env config, and the headers used across every step.

    <AccordionGroup>
      <Accordion title="Imports and env">
        ```typescript theme={null}
        import "dotenv/config";

        const DFLOW_METADATA_API_URL = process.env.DFLOW_METADATA_API_URL ?? "https://dev-prediction-markets-api.dflow.net";
        const DFLOW_API_KEY = process.env.DFLOW_API_KEY; // optional; not needed for dev endpoints

        const headers: HeadersInit = {};
        if (DFLOW_API_KEY) headers["x-api-key"] = DFLOW_API_KEY;
        ```
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Understand Market Statuses">
    Monitor a fixed lifecycle:
    **initialized → active → inactive → closed → determined → finalized**.

    Trade only when status is **active**. Redeem only when status is
    **determined** or **finalized**, and redemption is funded.

    <Info>
      Markets can move from `inactive` back to `active`, or from `inactive` to
      `closed`.
    </Info>
  </Step>

  <Step title="Get a Market and Read Status">
    Fetch a market by mint and read the `status` and `redemptionStatus`
    fields.

    <AccordionGroup>
      <Accordion title="Get Market Status by Mint">
        ```typescript theme={null}
        const outcomeMint = "OUTCOME_TOKEN_MINT_ADDRESS";

        const market = await fetch(
          `${DFLOW_METADATA_API_URL}/api/v1/market/by-mint/${outcomeMint}`,
          { headers }
        ).then((x) => x.json());

        if (!market.status) {
          throw new Error(`No market found for mint "${outcomeMint}". Replace with a real outcome or ledger mint. Response: ${JSON.stringify(market)}`);
        }

        console.log({
          status: market.status,
          redemptionStatus: market.accounts?.[market.settlementMint]?.redemptionStatus,
        });
        ```
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Filter Markets by Status">
    Filter markets to show only tradable or redeemable markets.

    <AccordionGroup>
      <Accordion title="Get Active Markets">
        ```typescript theme={null}
        const activeResponse = await fetch(
          `${DFLOW_METADATA_API_URL}/api/v1/markets?status=active&limit=200`,
          { headers }
        ).then((x) => x.json());

        console.log(activeResponse.markets?.length ?? 0);
        ```
      </Accordion>

      <Accordion title="Get Determined Markets">
        ```typescript theme={null}
        const determinedResponse = await fetch(
          `${DFLOW_METADATA_API_URL}/api/v1/markets?status=determined&limit=200`,
          { headers }
        ).then((x) => x.json());

        console.log(determinedResponse.markets?.length ?? 0);
        ```
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Gate Trading and Redemption">
    You gate trades to `active` markets and gate redemption to `determined` or
    `finalized` markets with `redemptionStatus = "open"`.

    <AccordionGroup>
      <Accordion title="Check Status Before Trading">
        ```typescript theme={null}
        if (market.status === "active") {
          // Allow trades
        } else {
          // Show market status to the user
        }
        ```
      </Accordion>

      <Accordion title="Check Status Before Redemption">
        ```typescript theme={null}
        const settlementAccount = market.accounts?.[market.settlementMint];

        if (
          (market.status === "determined" || market.status === "finalized") &&
          settlementAccount?.redemptionStatus === "open"
        ) {
          // Allow redemption
        }
        ```
      </Accordion>
    </AccordionGroup>
  </Step>
</Steps>

## Related Resources

<CardGroup cols={2}>
  <Card title="Market Lifecycle" href="/prediction-markets/market-lifecycle" icon="clock">
    States a market moves through and what each one allows.
  </Card>

  <Card title="Redeem Outcome Tokens" href="/prediction-markets/recipes/redeem-outcome-tokens" icon="check-circle">
    Redeem winning outcome tokens once a market is determined.
  </Card>

  <Card title="Buy Outcome Tokens" href="/prediction-markets/recipes/buy-outcome-tokens" icon="wallet">
    Open a prediction market position from any input token.
  </Card>
</CardGroup>

## API Routes

* [GET /api/v1/markets](/resources/metadata-api/markets/markets)
* [GET /api/v1/market/by-mint/{mint_address}](/resources/metadata-api/markets/market-by-mint)
