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

# 程式碼匯出

> 從你的 GraphQL 查詢產生 7 種程式語言的可直接使用程式碼片段

## 概述

程式碼匯出功能會將當前 GraphQL 查詢轉換為你所選語言的即用程式碼片段。生成程式碼包含完整 HTTP 請求配置 — 端點 URL、鑑權請求頭、查詢正文與變數 — 可直接複製到專案中使用。

***

## 支援的語言

<div style={{ overflowX: 'auto', width: '100%' }}>
  | 語言             | 格式              | 適用場景                       |
  | :------------- | :-------------- | :------------------------- |
  | **cURL**       | Shell 命令        | 終端快速驗證、CI/CD 指令碼           |
  | **Python**     | `requests` 庫    | 後端服務、資料管道、Jupyter Notebook |
  | **JavaScript** | `fetch` API     | 瀏覽器應用、Node.js 服務           |
  | **Go**         | `net/http`      | 後端微服務、CLI 工具               |
  | **Rust**       | `reqwest` crate | 高效能服務                      |
  | **Ruby**       | `net/http`      | Rails 應用、指令碼               |
  | **PHP**        | `cURL` 擴充套件     | PHP 後端、WordPress 外掛        |
</div>

***

## 如何匯出

<Steps>
  <Step title="編寫查詢">
    在編輯器中輸入或載入查詢。匯出前請確保查詢能成功執行。
  </Step>

  <Step title="點選 Export Code">
    點選工具欄中的 **Export Code** 按鈕。會彈出模態框，內含語言選擇與生成片段。
  </Step>

  <Step title="選擇語言">
    在下拉框中選擇目標語言，片段會立即更新。
  </Step>

  <Step title="複製片段">
    點選 **Copy** 按鈕，將生成程式碼複製到剪貼簿。
  </Step>
</Steps>

<Tip>
  匯出程式碼使用當前 IDE 配置中的 GraphQL 端點與 API Key。若你設定了自定義端點或請求頭，生成片段會反映這些設定。
</Tip>

***

## 示例片段

以下示例匯出一個簡單的 DEXTrades 查詢 — 拉取 Solana 上最近 10 筆 DEX 成交：

```graphql theme={null}
query {
  Solana {
    DEXTrades(
      limit: {count: 10}
      orderBy: {descending: Block_Time}
    ) {
      Block { Time }
      Transaction { Hash }
      Trade {
        Buy { Currency { MintAddress } Amount PriceInUSD }
        Sell { Currency { MintAddress } Amount }
        Dex { ProtocolName }
      }
    }
  }
}
```

<Tabs>
  <Tab title="cURL">
    ```bash theme={null}
    curl -X POST "https://graphql.chainstream.io/graphql" \
      -H "Content-Type: application/json" \
      -H "X-API-KEY: your_api_key" \
      -d '{
        "query": "{ Solana { DEXTrades(limit: {count: 10}, orderBy: {descending: Block_Time}) { Block { Time } Transaction { Hash } Trade { Buy { Currency { MintAddress } Amount PriceInUSD } Sell { Currency { MintAddress } Amount } Dex { ProtocolName } } } } }"
      }'
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    import requests

    url = "https://graphql.chainstream.io/graphql"
    headers = {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key",
    }
    query = """
    {
      Solana {
        DEXTrades(
          limit: {count: 10}
          orderBy: {descending: Block_Time}
        ) {
          Block { Time }
          Transaction { Hash }
          Trade {
            Buy { Currency { MintAddress } Amount PriceInUSD }
            Sell { Currency { MintAddress } Amount }
            Dex { ProtocolName }
          }
        }
      }
    }
    """

    response = requests.post(url, json={"query": query}, headers=headers)
    data = response.json()
    print(data)
    ```
  </Tab>

  <Tab title="JavaScript">
    ```javascript theme={null}
    const response = await fetch("https://graphql.chainstream.io/graphql", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key",
      },
      body: JSON.stringify({
        query: `{
          Solana {
            DEXTrades(
              limit: {count: 10}
              orderBy: {descending: Block_Time}
            ) {
              Block { Time }
              Transaction { Hash }
              Trade {
                Buy { Currency { MintAddress } Amount PriceInUSD }
                Sell { Currency { MintAddress } Amount }
                Dex { ProtocolName }
              }
            }
          }
        }`,
      }),
    });

    const data = await response.json();
    console.log(data);
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    package main

    import (
        "bytes"
        "encoding/json"
        "fmt"
        "io"
        "net/http"
    )

    func main() {
        payload := map[string]string{
            "query": `{ Solana { DEXTrades(limit: {count: 10}, orderBy: {descending: Block_Time}) { Block { Time } Transaction { Hash } Trade { Buy { Currency { MintAddress } Amount PriceInUSD } Sell { Currency { MintAddress } Amount } Dex { ProtocolName } } } } }`,
        }
        body, _ := json.Marshal(payload)

        req, _ := http.NewRequest("POST", "https://graphql.chainstream.io/graphql", bytes.NewBuffer(body))
        req.Header.Set("Content-Type", "application/json")
        req.Header.Set("X-API-KEY", "your_api_key")

        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            panic(err)
        }
        defer resp.Body.Close()

        result, _ := io.ReadAll(resp.Body)
        fmt.Println(string(result))
    }
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
    use serde_json::json;

    #[tokio::main]
    async fn main() -> Result<(), reqwest::Error> {
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers.insert("X-API-KEY", HeaderValue::from_static("your_api_key"));

        let body = json!({
            "query": r#"{ Solana { DEXTrades(limit: {count: 10}, orderBy: {descending: Block_Time}) { Block { Time } Transaction { Hash } Trade { Buy { Currency { MintAddress } Amount PriceInUSD } Sell { Currency { MintAddress } Amount } Dex { ProtocolName } } } } }"#
        });

        let client = reqwest::Client::new();
        let res = client
            .post("https://graphql.chainstream.io/graphql")
            .headers(headers)
            .json(&body)
            .send()
            .await?;

        println!("{}", res.text().await?);
        Ok(())
    }
    ```
  </Tab>
</Tabs>

***

## 匯出程式碼包含的內容

每個生成片段均包含：

| 組成部分               | 說明                                       |
| :----------------- | :--------------------------------------- |
| **Endpoint URL**   | `https://graphql.chainstream.io/graphql` |
| **Authentication** | 來自 IDE Headers 面板的 `X-API-KEY` 請求頭       |
| **Content-Type**   | `application/json`                       |
| **Query body**     | 編輯器中的完整 GraphQL 查詢                       |
| **Variables**      | 若 Variables 面板有值則會一併包含                   |

<Note>
  執行匯出程式碼前請將 `your_api_key` 替換為真實 API Key。出於安全考慮，IDE 不會在匯出片段中嵌入真實 Key。
</Note>

***

## 提示

* **先測再匯出** — 先在 IDE 中執行查詢確認可用。匯出程式碼會原樣複製查詢文字。
* **用變數引數化** — 使用 GraphQL 變數替代硬編碼值，匯出程式碼會同時包含 `variables` JSON 與查詢。
* **檢查依賴** — Python 片段依賴 `requests`，Rust 依賴 `reqwest`，Go 使用標準庫。請確保專案中已安裝所需包。
