> For the complete documentation index, see [llms.txt](https://dev.ada-anvil.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev.ada-anvil.io/guides/delegations/delegate-to-a-drep/deno-and-fetch.md).

# Deno & Fetch

## Prepare Delegation Transaction

**Payload**

{% code overflow="wrap" %}

```json
{
  "changeAddress": "addr...",
  "delegations": [
    {
      "type": "drep",
      "address": "addr..",
      "keyHash": "drep..."
    }
  ]
}
```

{% endcode %}

### Example (using Deno & fetch)

*Using a preprod wallet all value needed for a delegation*

{% code overflow="wrap" %}

```typescript
const DREP_ID = "drep13d6sxkyz6st9h65qqrzd8ukpywhr8swe9f6357qntgjqye0gttd";
const ADDR = "addr_test1qq7fc3ke49nkcsfglltut7apa9t3gdul4utwhxt6j2hdrw7pg4vk6erdshyhdj5xeq0vh8qdy34cpdfstvc8l9su8hgq679eew";
// See Authentication page for API key details.
const X_API_KEY = "testnet_EyrkvCWDZqjkfLSe1pxaF0hXxUcByHEhHuXIBjt9";
```

{% endcode %}

#### API POST Request Body

*Body Structure for a delegation to a DRep using the previously collected values.*

```typescript
const BODY = {
  changeAddress: ADDR,
  delegations: [
    {
      type: "drep",
      address: ADDR,
      keyHash: DREP_ID,
    },
  ],
};
```

#### Fetch Command with Deno

POST call with Fetch

{% code overflow="wrap" %}

```typescript
const response = await fetch(
  `https://preprod.api.ada-anvil.app/v2/services/transactions/build`,
  {
    method: "POST",
    headers: { "Content-Type": "application/json", "x-api-key": X_API_KEY },
    body: JSON.stringify(BODY),
  }
);

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

{% endcode %}

### Deno Command

```bash
deno run --allow-net delegate-drep.ts
```

### Output

{% code overflow="wrap" %}

```json
{
  "hash": "309afc6f9e06e70a0431eb929f4bacdfff463a0879c5d7daaef3357a4e6dbf64",
  "complete": "84a600d9010281825820f115e0c125b4023379833df656b5a5535d715f3eabb31e5bd5697d2604866b46000181825839003c9c46d9a9676c4128ffd7c5fba1e95714379faf16eb997a92aed1bbc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd0821a0ea25013a1581c360fd38656e5204f22ec058d18d5a90c18745ca8325e51d077c38a13a1581b616e76696c61706963697032355f3137333838363236313738393001021a0003f419031a04f5649904d901028282008200581cc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd083098200581cc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd08200581c8b75035882d4165bea8000c4d3f2c123ae33c1d92a751a78135a24020ed9010281581cc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd0a0f5f6",
  "stripped": "84a600d9010281825820f115e0c125b4023379833df656b5a5535d715f3eabb31e5bd5697d2604866b46000181825839003c9c46d9a9676c4128ffd7c5fba1e95714379faf16eb997a92aed1bbc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd0821a0ea25013a1581c360fd38656e5204f22ec058d18d5a90c18745ca8325e51d077c38a13a1581b616e76696c61706963697032355f3137333838363236313738393001021a0003f419031a04f5649904d901028282008200581cc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd083098200581cc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd08200581c8b75035882d4165bea8000c4d3f2c123ae33c1d92a751a78135a24020ed9010281581cc145596d646d85c976ca86c81ecb9c0d246b80b5305b307f961c3dd0a0f5f6",
  "witnessSet": "a0"
}
```

{% endcode %}

## The Whole File (Deno Version)

<details>

<summary>delegate-drep.ts</summary>

{% code overflow="wrap" %}

```typescript
const DREP_ID = "drep13d6sxkyz6st9h65qqrzd8ukpywhr8swe9f6357qntgjqye0gttd";
const ADDR = "addr_test1qq7fc3ke49nkcsfglltut7apa9t3gdul4utwhxt6j2hdrw7pg4vk6erdshyhdj5xeq0vh8qdy34cpdfstvc8l9su8hgq679eew";
// See Authentication page for API key details.
const X_API_KEY = "testnet_EyrkvCWDZqjkfLSe1pxaF0hXxUcByHEhHuXIBjt9";

const BODY = {
  changeAddress: ADDR,
  delegations: [
    {
      type: "drep",
      address: ADDR,
      keyHash: DREP_ID,
    },
  ],
};

const response = await fetch(
  `https://preprod.api.ada-anvil.app/v2/services/transactions/build`,
  {
    method: "POST",
    headers: { "Content-Type": "application/json", "x-api-key": X_API_KEY },
    body: JSON.stringify(BODY),
  }
);

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

export {};
```

{% endcode %}

</details>
