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

# Creating a NFT Contract

> Learn how to create a non-fungible token (NFT) using Contract Tools package

export const Github = ({url, start, end, fname, language, withSourceLink = true}) => {
  const [code, setCode] = useState(null);
  function toRaw(ref) {
    const fullUrl = ref.slice(ref.indexOf('https'));
    const [url] = fullUrl.split('#');
    const [org, repo, , branch, ...pathSeg] = new URL(url).pathname.split('/').slice(1);
    return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${pathSeg.join('/')}`;
  }
  async function fetchCode(url, fromLine, toLine) {
    let res;
    if (typeof window !== 'undefined') {
      const validUntil = localStorage.getItem(`${url}-until`);
      if (validUntil && Number(validUntil) > Date.now()) {
        res = localStorage.getItem(url);
      }
    }
    if (!res) {
      try {
        res = await (await fetch(url)).text();
        if (typeof window !== 'undefined') {
          localStorage.setItem(url, res);
          localStorage.setItem(`${url}-until`, String(Date.now() + 60000));
        }
      } catch {
        return 'Error fetching code, please try reloading';
      }
    }
    let body = res.split('\n');
    const from = fromLine ? Number(fromLine) - 1 : 0;
    const to = toLine ? Number(toLine) : body.length;
    body = body.slice(from, to);
    const precedingSpace = body.reduce((prev, line) => {
      if (line.length === 0) return prev;
      const spaces = line.match(/^\s+/);
      if (spaces) return Math.min(prev, spaces[0].length);
      return 0;
    }, Infinity);
    return body.map(line => line.slice(precedingSpace === Infinity ? 0 : precedingSpace)).join('\n');
  }
  function buildSourceUrl(url, start, end) {
    const base = url.split('#')[0];
    if (start && end) return `${base}#L${start}-L${end}`;
    if (start) return `${base}#L${start}`;
    return base;
  }
  useEffect(() => {
    const rawUrl = toRaw(url);
    fetchCode(rawUrl, start, end).then(res => setCode(res));
  }, [url, start, end]);
  const sourceUrl = buildSourceUrl(url, start, end);
  const fileName = fname ?? sourceUrl.split('/').pop();
  return <div className="my-5">
      {code === null ? <div>Loading...</div> : <CodeBlock language={language} filename={fileName} lines>
          {code}
        </CodeBlock>}
      {withSourceLink && <div className="flex justify-end" style={{
    marginTop: "-1rem"
  }}>
          <a href={sourceUrl} target="_blank" rel="noreferrer noopener" className="text-[0.6875rem] font-medium text-[#656d76] no-underline hover:text-[#1f2328] dark:text-[#8b949e] dark:hover:text-[#e6edf3]">
            See code on GitHub
          </a>
        </div>}
    </div>;
};

In this tutorial, you will create a non-fungible token (NFT) using the [NEAR SDK Contract Tools](https://github.com/near/near-sdk-contract-tools) package. This package is a collection of common tools and patterns to simplify smart contract development, including:

* Storage fee management
* Escrow pattern and derive macro
* Owner pattern and derive macro
* Pause pattern and derive macro
* Role-based access control
* Derive macros for [NEP standards](./standard)
  * [NEP-141](https://github.com/near/NEPs/blob/master/neps/nep-0141.md) (fungible token), extension [NEP-148](https://github.com/near/NEPs/blob/master/neps/nep-0148.md)
  * [NEP-145](https://github.com/near/NEPs/blob/master/neps/nep-0145.md) (storage management), and integrations for the fungible token and non-fungible token standards
  * [NEP-171](https://github.com/near/NEPs/blob/master/neps/nep-0171.md) (non-fungible token), extensions [NEP-177](https://github.com/near/NEPs/blob/master/neps/nep-0177.md), [NEP-178](https://github.com/near/NEPs/blob/master/neps/nep-0178.md), [NEP-181](https://github.com/near/NEPs/blob/master/neps/nep-0181.md)
  * [NEP-297](https://github.com/near/NEPs/blob/master/neps/nep-0297.md) (events)

***

## Introduction

While one can create a non-fungible token (NFT) contract from scratch using only the `near-sdk` and `near_contract_standards` (e.g. [NFT contract](https://github.com/near-examples/NFT)), a simpler approach is to use `near-sdk-contract-tools`.

`near-sdk-contract-tools` allows you to implement the minting/burning logic, access control, and other NFT standards by simply deriving macros on the contract's `struct`, as `OpenZeppelin` does for Ethereum contracts.

***

## Basic NFT Methods

To derive basic NFT methods to your smart contract, you need to derive the `NonFungibleToken` macro to the contract's `struct`:

<Github fname="lib.rs" language="rust" url="https://github.com/near-examples/nft-contract-tools/blob/main/src/lib.rs" start="9" end="12" />

This will bring all the basic NFT methods to the contract:

* `new`
* `contract_source_metadata`
* `nft_is_approved`
* `nft_metadata`
* `nft_supply_for_owner`
* `nft_token`
* `nft_tokens`
* `nft_tokens_for_owner`
* `nft_total_supply`
* `nft_approve`
* `nft_resolve_transfer`
* `nft_revoke`
* `nft_revoke_all`
* `nft_transfer`
* `nft_transfer_call`
* `storage_balance_bounds`
* `storage_balance_of`
* `storage_deposit`
* `storage_unregister`
* `storage_withdraw`

To bring basic owner methods to the contract, you also need to derive the `Owner` macro, which adds the following methods:

* `own_get_owner`
* `own_get_proposed_owner`
* `own_accept_owner`
* `own_propose_owner`
* `own_renounce_owner`

***

## Initialization

To initialize the basic NFT contract with a custom owner, metadata, and storage bounds, implement the `new` method:

<Github fname="lib.rs" language="rust" url="https://github.com/near-examples/nft-contract-tools/blob/main/src/lib.rs" start="14" end="31" />

***

## Transfer Hook

If you want to customize how the token transfer works (i.e., modify the `nft_transfer` method), you need to implement a hook. Hooks are a way to **wrap (inject code before and after)** component functions:

<Github fname="transfer_hook.rs" language="rust" url="https://github.com/near-examples/nft-contract-tools/blob/main/src/transfer_hook.rs" start="5" end="33" />

Then derive it to our contract struct:

<Github fname="lib.rs" language="rust" url="https://github.com/near-examples/nft-contract-tools/blob/main/src/lib.rs" start="9" end="12" />

***

## Minting

By default, the NFT standards do not include a minting method. However, you can easily mint tokens for the owner by implementing an `nft_mint` method:

<Github fname="mint.rs" language="rust" url="https://github.com/near-examples/nft-contract-tools/blob/main/src/mint.rs" start="10" end="40" />

<Tip>
  You can modify this method as you need, for example, to allow minting only when the contract is not paused (requires deriving [`Pausable`](https://github.com/near/near-sdk-contract-tools/tree/develop?tab=readme-ov-file#macro-combinations) hook), or to enable minting only to specific accounts with a certain role or from a whitelist with custom limitations.
</Tip>

***

## Burning

In the same way that minting is not included in the NFT standards, burning is also not included. However, you can also easily implement it.

To allow users to burn their tokens, you can add a `burn` method:

<Github fname="burn.rs" language="rust" url="https://github.com/near-examples/nft-contract-tools/blob/main/src/burn.rs" start="5" end="14" />

***

## Conclusion

Using `near-sdk-contract-tools` is a simple and flexible way to create an NFT contract with minimal boilerplate, which allows you to focus on the business logic.

You can further extend this contract with more features like pausing, role-based access control, escrow pattern, and more by deriving corresponding macros from the package.
