> ## Documentation Index
> Fetch the complete documentation index at: https://crossmint-wallets-docs-2-5.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Bring Your Own Contract

> Use Crossmint's infrastructure with your own smart contract

Crossmint allows you to use your own smart contract while leveraging our infrastructure for minting NFTs. This guide will walk you through the process of integrating your custom smart contract with Crossmint.

## Prerequisites

Before you can use your own smart contract with Crossmint, ensure that:

1. Your smart contract has a **free minting function** that allows Crossmint to mint NFTs without paying for gas.
2. Your smart contract complies with the **ERC721 standard** (for NFTs) or **ERC1155 standard** (for SFTs).

<Warning>
  Currently, this feature is only available for EVM chains. Support for Solana and other chains is coming soon.
</Warning>

## Integration Guide

### 1. Register Your Smart Contract

First, you need to register your smart contract with Crossmint:

```bash cURL theme={null}
curl --request POST \
  --url https://staging.crossmint.com/api/2022-06-09/collections/custom \
  --header 'Content-Type: application/json' \
  --header 'X-API-KEY: <api-key>' \
  --data '{
  "chain": "polygon",
  "contractAddress": "0x123...",
  "metadata": {
    "name": "My Custom Collection",
    "description": "A collection using my own smart contract"
  }
}'
```

### 2. Contract Review

After registering your contract, the Crossmint team will review it to ensure compatibility with our infrastructure. This typically takes 1-2 business days.

<Note>
  During the review process, we'll verify that your contract has the necessary free minting function and complies with
  the required standards.
</Note>

### 3. Mint NFTs

Once your contract is approved, you can start minting NFTs using the Crossmint API:

```bash cURL theme={null}
curl --request POST \
  --url https://staging.crossmint.com/api/2022-06-09/collections/{collectionId}/nfts \
  --header 'Content-Type: application/json' \
  --header 'X-API-KEY: <api-key>' \
  --data '{
  "recipient": "polygon:0x123...",
  "contractFunction": {
    "name": "mintTo",
    "params": [
      {
        "name": "to",
        "type": "address",
        "value": "$RECIPIENT_ADDRESS"
      },
      {
        "name": "tokenId",
        "type": "uint256",
        "value": "1"
      }
    ]
  }
}'
```

<Note>
  The `$RECIPIENT_ADDRESS` placeholder will be automatically replaced with the recipient's address by Crossmint.
</Note>

## Contract Function Parameters

When using your own smart contract, you need to specify the contract function to call and its parameters:

* **name**: The name of the function in your smart contract that mints NFTs.
* **params**: An array of parameters to pass to the function.
  * **name**: The parameter name as defined in your smart contract.
  * **type**: The parameter type (e.g., address, uint256, string).
  * **value**: The value to pass to the parameter.

<Warning>
  You cannot pass custom metadata as a parameter. Metadata is controlled at the smart contract level when using your
  own contract.
</Warning>

## Example: Minting with a Custom Contract

Here's an example of minting an NFT using a custom contract with a `mintTo` function:

```javascript theme={null}
const apiKey = "YOUR_API_KEY";
const collectionId = "your-custom-collection-id";
const env = "staging"; // or "www" for production
const recipient = "polygon:0x123..."; // Recipient's wallet address

const url = `https://${env}.crossmint.com/api/2022-06-09/collections/${collectionId}/nfts`;
const options = {
    method: "POST",
    headers: {
        accept: "application/json",
        "content-type": "application/json",
        "x-api-key": apiKey,
    },
    body: JSON.stringify({
        recipient,
        contractFunction: {
            name: "mintTo",
            params: [
                {
                    name: "to",
                    type: "address",
                    value: "$RECIPIENT_ADDRESS",
                },
                {
                    name: "tokenId",
                    type: "uint256",
                    value: "42",
                },
            ],
        },
    }),
};

fetch(url, options)
    .then((response) => response.json())
    .then((data) => console.log("NFT minted:", data))
    .catch((error) => console.error("Error minting NFT:", error));
```

## Best Practices

When using your own smart contract with Crossmint:

1. **Test Thoroughly**: Test your contract integration in the staging environment before moving to production.
2. **Function Visibility**: Ensure your minting function has the appropriate visibility and access controls.
3. **Gas Optimization**: Optimize your contract to minimize gas costs for minting operations.
4. **Error Handling**: Implement proper error handling in your contract to provide meaningful error messages.

## FAQs

<AccordionGroup>
  <Accordion title="Can I use an existing deployed contract?">
    Yes, you can use an existing deployed contract as long as it meets the prerequisites mentioned above.
  </Accordion>

  <Accordion title="What if my contract doesn't have a free minting function?">
    Your contract must have a function that allows Crossmint to mint NFTs without paying for gas. If your contract
    doesn't have this, you'll need to modify it or deploy a new version.
  </Accordion>

  <Accordion title="Can I use Crossmint's email delivery with my own contract?">
    Yes, you can still use Crossmint's email delivery system with your own contract. Just specify an email recipient
    in the mint request.
  </Accordion>

  <Accordion title="How do I handle token IDs with my custom contract?">
    Token ID management depends on your contract implementation. You can either pass a specific token ID as a
    parameter or let your contract handle token ID assignment internally.
  </Accordion>
</AccordionGroup>
