# Policy Units

In Cardano, "unit" typically refers to the policy ID plus the asset name in a single hex string that identifies a native token or NFT. Because you can have thousands of different tokens, each must be uniquely identified by a combination of:

* **Policy ID** (28 bytes, 56 hex characters).
* **Asset Name** (up to 32 bytes, variable hex length).

For CIP-25 or CIP-68 NFTs, you might see `unit` used in your minted assets or outputs. Similarly, when listing assets in an output, you specify:

```json
"unit": "<policyId+assetName>"
```

For example, if your `policyId` is:

```
1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417
```

and your asset name (in hex) is:

```
416e76696c54657374312038
```

the **Unit** becomes:

```
1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417416e76696c54657374312038
```

***

**Why Do Policy IDs and Asset Names Matter?**

1. **Token Gating and Access Control**

* Imagine you run a membership site or event. You want to grant access only to users who hold a specific NFT (like a “hotel keycard”).
* To validate their NFT, you check the **policy ID** (the “hotel”) and the **asset name** (the “room”). If a user’s wallet has the exact combination, they get in.

2. **Verification**

* If someone claims they hold a certain collectible or token, you can confirm by comparing their token’s **Unit** to the official “Unit” you expect.
* This prevents duplicates or fakes.

3. **Metadata and Searching**

* Explorers like preprod.cexplorer.io allow you to search by policy ID or asset name, letting you track how many tokens are minted, who holds them, and more.

4. **Minting and Burning**

* To mint or burn tokens, the blockchain needs to know exactly **which** asset under **which** policy you’re referring to.
* The policy ID and asset name form that unique identifier.

***

**How to Extract the Policy ID and Asset Name from a Unit**

If you have the **full unit string** (`policyId + assetName` in hex), you can slice it into two parts:

```typescript
const unit = "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417416e76696c54657374312038";

// First 56 characters (28 bytes in hex) is the policy ID:
const policyId = unit.slice(0, 56);

// The remaining portion is the asset name in hex:
const encodedAssetName = unit.slice(56);

// Optionally, you can convert the asset name from hex to plain text:
const assetName = Buffer.from(encodedAssetName, "hex").toString();
```

**Tip**: Some asset names contain non-ASCII characters, so you might see garbled text if you convert from hex incorrectly or if it’s truly a binary representation. Always check the token’s known format.

***

**Where to Find Your Policy ID and Asset Name**

**1. From a Blockchain Explorer**

1. Go to an explorer like Preprod CExplorer (for testnet) or CExplorer.io (for mainnet).
2. Paste the asset name or policy ID in the search bar (e.g., “AnvilTest1208”).
3. On the asset’s detail page, you’ll typically see:

* **Encoded Asset Name** (hex)
* **Policy ID** (hex string)

Once you have both, concatenate them to get the **unit**.

**2. From a Wallet (e.g., Eternl)**

1. Open your wallet interface.
2. Locate the NFT or token in question.
3. The wallet interface usually displays both:

* **Policy ID**
* **Asset Name** (in either plain text or hex)

Some wallets (like Eternl) also display the **unit** or a direct link to an explorer page.

***

TBD: Add practical example.

***

**Additional Tips and Best Practices**

1. **Hex vs. ASCII**

* Always confirm whether your asset name is stored in **hex** or **ASCII**.
* If you do comparisons in your code, stay consistent—comparing hex to ASCII won’t match.

2. **Policy Expiration**

* Some policies allow minting indefinitely; others have an expiration. Check the policy’s script if you plan to handle or mint new tokens under it.

3. **Handling Special Characters**

* Asset names may include emojis or non-English text. The hex representation ensures compatibility, but the plain text might look odd.

4. **Batching**

* For dApps dealing with numerous tokens, consider how you’ll fetch all these units efficiently (paging, caching, etc.).

5. **Don’t Overlook Testnets**

* If you’re new to token gating or minted assets, test your approach on **Preprod** or **Preview** first. Real ADA isn’t at risk there.

***

**Summary**

* `Unit = policyId + assetName` (in hex).
* **Why It Matters**: It’s the unique identifier for any Cardano-native asset, crucial for gating, verification, minting, and more.
* **Where to Get Them**: Use explorers, your wallet, or direct blockchain APIs.
* **Hotel Keycard Analogy**: The policy ID is the “hotel,” and the asset name is the “room.” Checking them together ensures you grant access to the right person holding the right token.

With a solid grasp of **Units**, **Policy IDs**, and **Asset Names**, you can confidently build sophisticated Cardano applications—whether that’s token-gated dApps, NFT marketplaces, or membership sites—knowing exactly which assets users hold and how to verify them securely.

***

**Next Steps**

Learn about Utilities Functions to simplify asset handling (e.g., `unitToName()`, `nameToUnit()`, etc.). Jump into Transaction for guidance on how to send assets, sign transactions, or mint new tokens with Anvil. Explore advanced NFT & FT documentation to create, distribute, and manage your custom assets.


---

# Agent Instructions: 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://dev.ada-anvil.io/guides/nft-and-ft/native-scripts/policy-units.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.
