# Deno & Fetch

## Introduction

This example demonstrates how to create a transaction with [CIP-20](https://cips.cardano.org/cip/CIP-20) metadata using the Anvil API. CIP-20 provides a standardized way to include human-readable messages in Cardano transactions using metadata label 674.

For more information about transactions, please refer to the [Transaction Overview](https://github.com/Cardano-Forge/anvil-api/blob/main/docs/guides/transaction/transaction/README.md).

## Objectives

This example creates a transaction with a CIP-20 compliant message on the Preprod network.

## Requirements

* A Cardano wallet with ADA
* A valid API key
* Deno or Node.js environment

## API Request Structure

### Payload Format

```json
{
  "changeAddress": "addr_sender...",
  "message": "Your message" // String or array of strings
}
```

## Message Format Options

### Single String Message

```json
{
  "changeAddress": "addr_sender...",
  "message": "Receipt 42"
}
```

### Array of Strings

```json
{
  "changeAddress": "addr_sender...",
  "message": ["Receipt 42", "Minted by Anvil"]
}
```

### Auto-split for Long Messages

For messages exceeding 64 bytes, the API automatically splits them:

```json
{
  "changeAddress": "addr_sender...",
  "message": "A very long message to demonstrate the auto-split feature. I need more words"
}
```

**Will be formatted as:**

```json
{
  "msg": [
    "A very long message to demonstrate the auto-split feature. I nee",
    "d more words"
  ]
}
```

## Implementation

### Configuration and Parameters (using Deno and Fetch)

*Using a preprod wallet for the transaction*

```typescript
const ADDRESS =
  "addr_test1qrydyk6uw6cehk5u3zspyz3dhnwzmhfls2fp42vv5dv9g2z3885pg4kpkn30ptezc855lu3w5ey93zcr5lrezjmwkftqg8xvge";
// See Authentication page for API key details.
const X_API_KEY = "testnet_EyrkvCWDZqjkfLSe1pxaF0hXxUcByHEhHuXIBjt9";
const API_URL = "https://preprod.api.ada-anvil.app/v2/services";
const HEADERS = { "Content-Type": "application/json", "x-api-key": X_API_KEY };
```

### Request Body

*Body Structure for creating a transaction with CIP-20 metadata*

```typescript
const BODY = {
  changeAddress: ADDRESS,
  message: ["Receipt 42", "2025-01-24"],
};
```

### API Call (using Deno and Fetch)

Basic POST call with Fetch

{% code title="cip-20.ts" overflow="wrap" %}

```typescript
const response = await fetch(`${API_URL}/transactions/build`, {
  method: "POST",
  headers: HEADERS,
  body: JSON.stringify(BODY),
});

console.log(await response.json());
```

{% endcode %}

### Deno Command

```bash
deno run --allow-net cip-20.ts
```

### Expected Output

{% code overflow="wrap" %}

```json
{
  "hash": "b2431ba925d55f3a117b4dd7d388b21277c6f89a27bfdc3895c10cae1dfcca7f",
  "complete": "84a700d9010282825820557a0804947569e286b0c98d859858bb5fc0697dc8fbb86b1c5d28ff7152a2a001825820593ad1e4983410a61b092d5119f92bd9b66d4a4eb4bdaf61c8cad6f8b9dffc1c010182a300581d6001687d507bb21217905bb35686ae6b373b26ce1ebfe2bc6db1caad5f011a00155cc0028201d8184a49616e76696c2d74616782583900c8d25b5c76b19bda9c88a0120a2dbcdc2ddd3f82921aa98ca35854285139e81456c1b4e2f0af22c1e94ff22ea648588b03a7c7914b6eb2561b000000018d343f3e021a0003098d031a052213630758201bd3787203ca834841a6e263f524989c7a2d8d340ae67098cc9ee225e7bb5df2081a0521f7430ed9010281581c01687d507bb21217905bb35686ae6b373b26ce1ebfe2bc6db1caad5fa100d9010281825820cfd1cfc82efa73c76302b9103f81b4c1fb2e9dc0ed39ab45039fb6d4c3fbe7a35840f32d960934a6511cb120a61dd8283a60ded05026e4b9e682aa3583a3c7d6be84b0749c3e86e4f226f5aa461869b8b9dc2fca81684a872f101691eedbbcd92c0af5a11902a2a1636d7367826a526563656970742034326a323032352d30312d3234",
  "stripped": "84a700d9010282825820557a0804947569e286b0c98d859858bb5fc0697dc8fbb86b1c5d28ff7152a2a001825820593ad1e4983410a61b092d5119f92bd9b66d4a4eb4bdaf61c8cad6f8b9dffc1c010182a300581d6001687d507bb21217905bb35686ae6b373b26ce1ebfe2bc6db1caad5f011a00155cc0028201d8184a49616e76696c2d74616782583900c8d25b5c76b19bda9c88a0120a2dbcdc2ddd3f82921aa98ca35854285139e81456c1b4e2f0af22c1e94ff22ea648588b03a7c7914b6eb2561b000000018d343f3e021a0003098d031a052213630758201bd3787203ca834841a6e263f524989c7a2d8d340ae67098cc9ee225e7bb5df2081a0521f7430ed9010281581c01687d507bb21217905bb35686ae6b373b26ce1ebfe2bc6db1caad5fa0f5f6",
  "witnessSet": "a100d9010281825820cfd1cfc82efa73c76302b9103f81b4c1fb2e9dc0ed39ab45039fb6d4c3fbe7a35840f32d960934a6511cb120a61dd8283a60ded05026e4b9e682aa3583a3c7d6be84b0749c3e86e4f226f5aa461869b8b9dc2fca81684a872f101691eedbbcd92c0a",
  "auxiliaryData": "a11902a2a1636d7367826a526563656970742034326a323032352d30312d3234"
}
```

{% endcode %}

## The Whole File (Deno Version)

<details>

<summary>cip-20.ts</summary>

{% code overflow="wrap" %}

```typescript
// deno run --allow-net cip-20.ts
const ADDRESS =
  "addr_test1qrydyk6uw6cehk5u3zspyz3dhnwzmhfls2fp42vv5dv9g2z3885pg4kpkn30ptezc855lu3w5ey93zcr5lrezjmwkftqg8xvge";
// See Authentication page for API key details.
const X_API_KEY = "testnet_EyrkvCWDZqjkfLSe1pxaF0hXxUcByHEhHuXIBjt9";
const API_URL = "https://preprod.api.ada-anvil.app/v2/services";
const HEADERS = { "Content-Type": "application/json", "x-api-key": X_API_KEY };
const BODY = {
  changeAddress: ADDRESS,
  message: ["Receipt 42", "2025-03-12"],
};
const response = await fetch(`${API_URL}/transactions/build`, {
  method: "POST",
  headers: HEADERS,
  body: JSON.stringify(BODY),
});
console.log(await response.json());
```

{% endcode %}

</details>
