> For the complete documentation index, see [llms.txt](https://docs.squidrouter.exchange/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.squidrouter.exchange/api-and-sdk-integration/api/cross-chain-nft-purchase-example.md).

# Cross-chain NFT Purchase Example

Robust documentation for a cross-chain NFT Purchase coming soon.

Please refer to this example of purchasing an [NFT on Polygon from any chain](https://github.com/0xsquid/examples/blob/main/V2/api/oldExamples/mintPolygonNftFromAnyChain/src/index.ts) from our [example repo.](https://github.com/0xsquid/examples/tree/main)

```typescript
// Import necessary libraries
import { ethers } from "ethers";
import axios from "axios";

// Load environment variables from the .env file
import * as dotenv from "dotenv";
dotenv.config();

const privateKey: string = process.env.PRIVATE_KEY!;
const integratorId: string = process.env.INTEGRATOR_ID!; // get one at https://form.typeform.com/to/cqFtqSvX
const rpcEndpoint: string = process.env.RPC_ENDPOINT!;
const nftContractAddress: string = process.env.NFT_CONTRACT_ADDRESS!;

// Define chain and token addresses
const fromChainId = "1"; // Define departing chain, set to Ethereum by default
const polygonId = "137"; // Polygon
const nativeToken = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Define departing token

// Define amount to be sent
const amount = "10000000000000000";

// Import necessary ABI's
import nftContractAbi from "../abi/squidEasterEggNftAbi";
import erc20Abi from "../abi/erc20Abi";

// Set up JSON RPC provider and signer for source chain (Ethereum)
const provider = new ethers.providers.JsonRpcProvider(rpcEndpoint);
const signer = new ethers.Wallet(privateKey, provider);

const getRoute = async (params: any) => {
  try {
    const result = await axios.post(
      "https://v2.api.squidrouter.exchange/v2/route",
      params,
      {
        headers: {
          "x-integrator-id": integratorId,
          "Content-Type": "application/json",
        },
      }
    );
    const requestId = result.headers["x-request-id"];
    return { data: result.data, requestId: requestId };
  } catch (error) {
    // Log the error response if it's available.
    if (error.response) {
      console.error("API error:", error.response.data);
    }
    console.error("Error with parameters:", params);
    throw error;
  }
};

const getStatus = async (params: any) => {
  try {
    const result = await axios.get("https://v2.api.squidrouter.exchange/v2/status", {
      params: {
        transactionId: params.transactionId,
        requestId: params.requestId,
        fromChainId: params.fromChainId,
        toChainId: params.toChainId,
      },
      headers: {
        "x-integrator-id": integratorId,
      },
    });
    return result.data;
  } catch (error) {
    if (error.response) {
      console.error("API error:", error.response.data);
    }
    console.error("Error with parameters:", params);
    throw error;
  }
};

// Create contract interfaces and encode calldata
const nftContractInterface = new ethers.utils.Interface(nftContractAbi);
const mintEncodedData = nftContractInterface.encodeFunctionData("mint", [
  signer.address,
]);

const erc20ContractInterface = new ethers.utils.Interface(erc20Abi);
const transferRemainingBalanceEncodeData =
  erc20ContractInterface.encodeFunctionData("transfer", [signer.address, "0"]);

(async () => {
  // Set up parameters for swapping tokens and minting NFT on Polygon
  const params = {
    fromAddress: signer.address,
    fromChain: fromChainId,
    fromToken: nativeToken,
    fromAmount: amount,
    toChain: polygonId,
    toToken: nativeToken,
    toAddress: signer.address,
    slippage: 1,   //optional, Squid will dynamically calculate if removed
    quoteOnly: false,
    // Customize contract call for minting NFT on Polygon
    postHooks: [
      {
        callType: 0, // SquidCallType.DEFAULT
        target: nftContractAddress,
        value: "0",
        callData: mintEncodedData,
        payload: {
          tokenAddress: nativeToken,
          inputPos: 1,
        },
        estimatedGas: "50000",
      },
      {
        callType: 1, // SquidCallType.FULL_TOKEN_BALANCE
        target: nativeToken,
        value: "0",
        callData: transferRemainingBalanceEncodeData,
        payload: {
          tokenAddress: nativeToken,
          inputPos: 1,
        },
        estimatedGas: "50000",
      },
    ],
  };

  console.log("Parameters:", params);

  // Get the swap route using Squid API
  const routeResult = await getRoute(params);
  const route = routeResult.data.route;
  const requestId = routeResult.requestId;
  console.log("Calculated route:", route);
  console.log("requestId:", requestId);

  const transactionRequest = route.transactionRequest;

  // Execute the swap and minting transaction
  const contract = new ethers.Contract(
    transactionRequest.targetAddress,
    nftContractAbi,
    signer
  );
  const tx = await contract.send(transactionRequest.data, {
    value: transactionRequest.value,
    gasPrice: await provider.getGasPrice(),
    gasLimit: transactionRequest.gasLimit,
  });
  const txReceipt = await tx.wait();

  // Show the transaction receipt with Axelarscan link
  const axelarScanLink =
    "https://axelarscan.io/gmp/" + txReceipt.transactionHash;
  console.log(`Finished! Check Axelarscan for details: ${axelarScanLink}`);

  // Wait a few seconds before checking the status
  await new Promise((resolve) => setTimeout(resolve, 5000));

  // Retrieve the transaction's route status
  const getStatusParams = {
    transactionId: txReceipt.transactionHash,
    requestId: requestId,
    fromChainId: fromChainId,
    toChainId: polygonId,
  };
  const status = await getStatus(getStatusParams);

  // Display the route status
  console.log(`Route status: ${status.squidTransactionStatus}`);
})();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.squidrouter.exchange/api-and-sdk-integration/api/cross-chain-nft-purchase-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
