# TZIP-12 Token Metadata

> Source: https://taquito.io/docs/24.2.0/tzip12

The `@taquito/tzip12` package allows retrieving metadata associated with tokens of FA2 contract. You can find more information about the TZIP-12 standard [here](https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-12/tzip-12.md).

## How to use the tzip12 package

The package can act as an extension to the well-known Taquito contract abstraction.

1. **We first need to create an instance of `Tzip12Module` and add it as an extension to our `TezosToolkit`**

The constructor of the `Tzip12Module` takes an optional `MetadataProvider` as a parameter. When none is passed, the default `MetadataProvider` of Taquito is instantiated, and the default handlers (`HttpHandler,` `IpfsHandler,` and `TezosStorageHandler`) are used.

```js

const Tezos = new TezosToolkit('rpcUrl');
Tezos.addExtension(new Tzip12Module());

```

*Note that the `Tzip16Module` and `Tzip12Module` use the same `MetadataProvider`. If you have already set `Tezos.addExtension(new Tzip16Module());`, you can omit this step.*

2. **Use the `tzip12` function to extend a contract abstraction**

```js
const contract = await Tezos.contract.at("contractAddress", tzip12)
```

**The compose function**
The contract abstraction can also be extended to a `Tzip12ContractAbstraction` and a `Tzip16ContractAbstraction` (at the same time) by using the `compose` function.
Thus, all methods of the `ContractAbstraction`, `Tzip12ContractAbstraction` and `Tzip16ContractAbstraction` classes will be available on the contract abstraction instance.

```js

const contract = await Tezos.contract.at('contractAddress', compose(tzip16, tzip12));

await contract.storage(); // ContractAbstraction method
await contract.tzip12().getTokenMetadata(1); // Tzip12ContractAbstraction method
await contract.tzip16().getMetadata(); // Tzip16ContractAbstraction method
```

## Get the token metadata

There are two scenarios to obtain the metadata of a token:
1. They can be obtained from executing an off-chain view named `token_metadata` present in the contract metadata
2. or from a big map named `token_metadata` in the contract storage.

The `getTokenMetadata` method of the `Tzip12ContractAbstraction` class will find the token metadata with precedence for the off-chain view, if there is one, as specified in the standard.

The `getTokenMetadata` method returns an object matching this interface :
```
interface TokenMetadata {
    token_id: number,
    decimals: number
    name?: string,
    symbol?: string,
}
```

:::note
If additional metadata values are provided for a token_id, they will also be returned.
:::

Here is a flowchart that summarizes the logic perform internally when calling the `getTokenMetadata` method:

![Flowchart](/img/FlowchartGetTokenMetadata.png)

**Note: If there is a URI in the token_info map and other keys/values in the map, all properties will be returned (properties fetched from the URI and properties found in the map). If the same key is found at the URI location and in the map token_info and that their value is different, precedence is accorded to the value from the URI.*

### Example where the token metadata are obtained from an off-chain view `token_metadata`

#### Contract API

```js
// import { TezosToolkit, compose } from '@taquito/taquito';
// import { Tzip12Module, tzip12 } from "@taquito/tzip12";
// import { tzip16 } from "@taquito/tzip16";
// const Tezos = new TezosToolkit('rpc_url');

await Tezos.addExtension(new Tzip12Module());

const contractAddress = "KT1PSz2rUJQCAGC8ptvmyErfsScaQm9tbuUv";
const tokenId = 1;

try {
  const contract = await Tezos.contract.at(contractAddress, compose(tzip12, tzip16));
  console.log(`Fetching the token metadata for the token ID ${tokenId}...`);
  const tokenMetadata = await contract.tzip12().getTokenMetadata(tokenId);
  console.log(JSON.stringify(tokenMetadata, null, 2));
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```

  
#### Wallet API

```js
// import { TezosToolkit, compose } from '@taquito/taquito';
// import { Tzip12Module, tzip12 } from "@taquito/tzip12";
// import { tzip16 } from "@taquito/tzip16";
// const Tezos = new TezosToolkit('rpc_url');

Tezos.addExtension(new Tzip12Module());

const contractAddress = "KT1PSz2rUJQCAGC8ptvmyErfsScaQm9tbuUv";
const tokenId = 1;

try {
  const wallet = await Tezos.wallet.at(contractAddress, compose(tzip12, tzip16));
  console.log(`Fetching the token metadata for the token ID ${tokenId}...`);
  const tokenMetadata = await wallet.tzip12().getTokenMetadata(tokenId);
  console.log(JSON.stringify(tokenMetadata, null, 2));
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```
  

The same result can also be obtained by calling the off-chain view `token_metadata` using the `taquito-tzip16` package:

#### Contract API

```js
// import { TezosToolkit } from '@taquito/taquito';
// import { Tzip16Module, tzip16, bytesToString } from "@taquito/tzip16";
// const Tezos = new TezosToolkit('rpc_url');

Tezos.addExtension(new Tzip16Module());

const contractAddress = "KT1PSz2rUJQCAGC8ptvmyErfsScaQm9tbuUv";
const tokenId = 1;

try {
  const contract = await Tezos.contract.at(contractAddress, tzip16);
  console.log(`Initialising the views for ${contractAddress}...`);
  const views = await contract.tzip16().metadataViews();
  const result = await views['token_metadata']().executeView(tokenId);
  console.log('Result of the view token_metadata:');
  console.log(`name: ${bytesToString((Object.values(result)[1]).get('name'))}`);
  console.log(`decimals: ${bytesToString((Object.values(result)[1]).get('decimals'))}`);
  console.log(`symbol: ${bytesToString((Object.values(result)[1]).get('symbol'))}`);
  console.log(`extra: ${bytesToString((Object.values(result)[1]).get('extra'))}`);
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```

  
#### Wallet API

```js
// import { TezosToolkit } from '@taquito/taquito';
// import { Tzip16Module, tzip16, bytesToString } from "@taquito/tzip16";
// const Tezos = new TezosToolkit('rpc_url');

Tezos.addExtension(new Tzip16Module());

const contractAddress = "KT1PSz2rUJQCAGC8ptvmyErfsScaQm9tbuUv";
const tokenId = 1;

try {
  const wallet = await Tezos.wallet.at(contractAddress, tzip16);
  console.log(`Initialising the views for ${contractAddress}...`);
  const views = await wallet.tzip16().metadataViews();
  const result = await views['token_metadata']().executeView(tokenId);
  console.log('Result of the view token_metadata:');
  console.log(`name: ${bytesToString((Object.values(result)[1]).get('name'))}`);
  console.log(`decimals: ${bytesToString((Object.values(result)[1]).get('decimals'))}`);
  console.log(`symbol: ${bytesToString((Object.values(result)[1]).get('symbol'))}`);
  console.log(`extra: ${bytesToString((Object.values(result)[1]).get('extra'))}`);
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```
  

*Note that an off-chain view `all-tokens` should also be present in the contract metadata allowing the user to know with which token ID the `token_metadata` can be called.*

### Example where the token metadata are found in the big map `%token_metadata`

:::note
To be [Tzip-012 compliant](https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-12/tzip-12.md#token-metadata-storage-access), the type of the big map `%token_metadata` in the storage of the contract should match the following type:

  
#### michelson

```
(big_map %token_metadata nat
  (pair (nat %token_id)
    (map %token_info string bytes)))
```

  
  
#### jsonMichelson

```
prim: 'big_map',
    args: [
        { prim: 'nat' },
        { prim: 'pair', args: [
            { prim: 'nat' , annots: ['%token_id']},
            { prim: "map", args: [{ prim: 'string' }, { prim: 'bytes' }], annots: ['%token_info'] }] }],
    annots: ['%token_metadata']
```

  

Otherwise, the token metadata won't be found by the `getTokenMetadata` method, and a `TokenMetadataNotFound` error will be thrown.
:::

#### Contract API

```js
// import { TezosToolkit } from '@taquito/taquito';
// import { Tzip12Module, tzip12 } from "@taquito/tzip12";
// const Tezos = new TezosToolkit('rpc_url');

Tezos.addExtension(new Tzip12Module());

const contractAddress = "KT1PSz2rUJQCAGC8ptvmyErfsScaQm9tbuUv";
const tokenId = 1;

try {
  const contract = await Tezos.contract.at(contractAddress, tzip12);
  console.log(`Fetching the token metadata for the token ID ${tokenId} of ${contractAddress}...`);
  const tokenMetadata = await contract.tzip12().getTokenMetadata(tokenId);
  console.log(JSON.stringify(tokenMetadata, null, 2));
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```

  
#### Wallet API

```js
// import { TezosToolkit } from '@taquito/taquito';
// import { Tzip12Module, tzip12 } from "@taquito/tzip12";
// const Tezos = new TezosToolkit('rpc_url');

Tezos.addExtension(new Tzip12Module());

const contractAddress = "KT1PSz2rUJQCAGC8ptvmyErfsScaQm9tbuUv";
const tokenId = 1;

try {
  const wallet = await Tezos.wallet.at(contractAddress, tzip12);
  console.log(`Fetching the token metadata for the token ID ${tokenId} of ${contractAddress}...`);
  const tokenMetadata = await wallet.tzip12().getTokenMetadata(tokenId);
  console.log(JSON.stringify(tokenMetadata, null, 2));
} catch (error) {
  console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```
  

#### For more information on the contracts used in the examples:

integration-tests/tzip12-token-metadata.spec.ts
