|
| 1 | +--- |
| 2 | +title: Get Token Metadata |
| 3 | +description: |
| 4 | + Learn how to read and parse token metadata from the Solana blockchain using the gill JavaScript |
| 5 | + library. |
| 6 | +--- |
| 7 | + |
| 8 | +Every Solana token can have metadata associated with it, including a name, symbol, URI pointing to |
| 9 | +off-chain data, and more. The approach to reading this metadata depends on which token program was |
| 10 | +used to create the token: |
| 11 | + |
| 12 | +- **Token Metadata Program (legacy):** Metadata lives in a separate PDA account managed by the |
| 13 | + Metaplex Token Metadata program. |
| 14 | +- **Token Extensions (Token22):** Metadata is stored inline on the mint account as an extension. |
| 15 | + |
| 16 | +This guide demonstrates how to fetch token metadata using the |
| 17 | +[`gill` package](https://www.npmjs.com/package/gill), covering both approaches. |
| 18 | + |
| 19 | +## Install gill |
| 20 | + |
| 21 | +Install gill using the core `gill` library: |
| 22 | + |
| 23 | +```package-install |
| 24 | +gill |
| 25 | +``` |
| 26 | + |
| 27 | +import { PackageBadges } from "@/components/package-badges"; |
| 28 | + |
| 29 | +<PackageBadges packageName="gill" /> |
| 30 | + |
| 31 | +## Create an RPC connection |
| 32 | + |
| 33 | +In order to fetch data from the Solana blockchain, you will need a client connection. You can easily |
| 34 | +create a Solana client connection using the `createSolanaClient()` function. |
| 35 | + |
| 36 | +The `urlOrMoniker` can be either a Solana network moniker (e.g. `devnet`, `mainnet`, `localnet`) or |
| 37 | +a full URL of your RPC provider. |
| 38 | + |
| 39 | +```ts twoslash |
| 40 | +import { createSolanaClient } from "gill"; |
| 41 | + |
| 42 | +const { rpc } = createSolanaClient({ |
| 43 | + urlOrMoniker: "mainnet", // `devnet`, `localnet`, etc |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +<Callout title="Public RPC endpoints are subject to rate limits"> |
| 48 | + Using a Solana moniker will connect to the public RPC endpoints. These are subject to rate limits |
| 49 | + and should not be used in production applications. Applications should find their own RPC provider |
| 50 | + and the URL provided from them. |
| 51 | +</Callout> |
| 52 | + |
| 53 | +## Token Metadata Program (legacy tokens) |
| 54 | + |
| 55 | +For tokens created with the original Token Program and the Metaplex Token Metadata program, metadata |
| 56 | +is stored in a separate PDA account derived from the token's mint address. |
| 57 | + |
| 58 | +### Derive the metadata address |
| 59 | + |
| 60 | +Use `getTokenMetadataAddress()` from `gill/programs` to derive the metadata PDA from the mint |
| 61 | +address: |
| 62 | + |
| 63 | +```ts |
| 64 | +import { address } from "gill"; |
| 65 | +import { getTokenMetadataAddress } from "gill/programs"; |
| 66 | + |
| 67 | +const mint = address("So11111111111111111111111111111111111111112"); |
| 68 | + |
| 69 | +const metadataAddress = await getTokenMetadataAddress(mint); |
| 70 | +``` |
| 71 | + |
| 72 | +### Fetch the metadata account |
| 73 | + |
| 74 | +Use `fetchMetadata()` from `gill/programs` to fetch and decode the metadata account: |
| 75 | + |
| 76 | +```ts |
| 77 | +import { fetchMetadata } from "gill/programs"; |
| 78 | + |
| 79 | +const metadata = await fetchMetadata(rpc, metadataAddress); |
| 80 | + |
| 81 | +console.log("Name:", metadata.data.name); |
| 82 | +console.log("Symbol:", metadata.data.symbol); |
| 83 | +console.log("URI:", metadata.data.uri); |
| 84 | +console.log("Seller fee:", metadata.data.sellerFeeBasisPoints); |
| 85 | +console.log("Creators:", metadata.data.creators); |
| 86 | +``` |
| 87 | + |
| 88 | +The metadata account also contains additional fields: |
| 89 | + |
| 90 | +```ts |
| 91 | +console.log("Update authority:", metadata.updateAuthority); |
| 92 | +console.log("Is mutable:", metadata.isMutable); |
| 93 | +console.log("Collection:", metadata.collection); |
| 94 | +console.log("Token standard:", metadata.tokenStandard); |
| 95 | +``` |
| 96 | + |
| 97 | +<Callout title="Use fetchMaybeMetadata for optional lookups"> |
| 98 | + If you are unsure whether a metadata account exists for a given mint, use `fetchMaybeMetadata()` |
| 99 | + instead. It returns `null` if the account does not exist, rather than throwing an error. |
| 100 | +</Callout> |
| 101 | + |
| 102 | +```ts |
| 103 | +import { fetchMaybeMetadata } from "gill/programs"; |
| 104 | + |
| 105 | +const maybeMeta = await fetchMaybeMetadata(rpc, metadataAddress); |
| 106 | + |
| 107 | +if (maybeMeta.exists) { |
| 108 | + console.log("Name:", maybeMeta.data.name); |
| 109 | +} else { |
| 110 | + console.log("No metadata account found"); |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Token Extensions (Token22) |
| 115 | + |
| 116 | +For tokens created with the Token Extensions program (Token22), metadata can be stored directly on |
| 117 | +the mint account as an extension. No separate PDA is needed. |
| 118 | + |
| 119 | +### Fetch the mint account |
| 120 | + |
| 121 | +Use `fetchMint()` from `gill/programs` to fetch the mint account, which includes any extensions: |
| 122 | + |
| 123 | +```ts |
| 124 | +import { address } from "gill"; |
| 125 | +import { fetchMint } from "gill/programs"; |
| 126 | + |
| 127 | +const mint = address("your-token22-mint-address"); |
| 128 | + |
| 129 | +const mintAccount = await fetchMint(rpc, mint); |
| 130 | +``` |
| 131 | + |
| 132 | +### Find the TokenMetadata extension |
| 133 | + |
| 134 | +The mint account's `extensions` array contains all enabled extensions. Use the `isExtension()` type |
| 135 | +guard from `gill/programs` to find the `TokenMetadata` extension: |
| 136 | + |
| 137 | +```ts |
| 138 | +import { fetchMint, isExtension } from "gill/programs"; |
| 139 | + |
| 140 | +const mintAccount = await fetchMint(rpc, mint); |
| 141 | +const tokenMetadata = mintAccount.data.extensions?.find((ext) => isExtension("TokenMetadata", ext)); |
| 142 | + |
| 143 | +if (tokenMetadata && tokenMetadata.__kind === "TokenMetadata") { |
| 144 | + console.log("Name:", tokenMetadata.name); |
| 145 | + console.log("Symbol:", tokenMetadata.symbol); |
| 146 | + console.log("URI:", tokenMetadata.uri); |
| 147 | + console.log("Update authority:", tokenMetadata.updateAuthority); |
| 148 | + console.log("Additional metadata:", tokenMetadata.additionalMetadata); |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +<Callout title="Token22 metadata differences"> |
| 153 | + Token Extensions metadata includes an `additionalMetadata` field, which is a `Map` of arbitrary |
| 154 | + key-value string pairs. This is different from the legacy Token Metadata Program, which uses |
| 155 | + structured fields like `creators` and `collection`. |
| 156 | +</Callout> |
| 157 | + |
| 158 | +## Fetching the off-chain JSON metadata |
| 159 | + |
| 160 | +Both the legacy Token Metadata Program and Token Extensions store a `uri` field that points to an |
| 161 | +off-chain JSON file. This JSON typically contains the token's image, description, attributes, and |
| 162 | +other rich metadata. |
| 163 | + |
| 164 | +Fetching this data is a standard HTTP request, not a Solana RPC call: |
| 165 | + |
| 166 | +```ts |
| 167 | +const response = await fetch(uri); |
| 168 | +const offChainMetadata = await response.json(); |
| 169 | + |
| 170 | +console.log("Image:", offChainMetadata.image); |
| 171 | +console.log("Description:", offChainMetadata.description); |
| 172 | +``` |
| 173 | + |
| 174 | +The off-chain JSON format generally follows the |
| 175 | +[Metaplex Token Metadata Standard](https://developers.metaplex.com/token-metadata/token-standard), |
| 176 | +which includes fields like `image`, `description`, `attributes`, `external_url`, and more. |
0 commit comments