# Deno & Fetch

## Prepare Pool Delegation Transaction

**Payload**

{% code overflow="wrap" %}

```json
{
  "changeAddress": "addr_test1...",
  "delegations": [
    {
      "type": "pool",
      "address": "addr_test1...",
      "keyHash": "pool1z5uq..."
    }
  ]
}
```

{% endcode %}

### Example (using Deno & fetch)

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

{% code overflow="wrap" %}

```typescript
const POOL_ID = "pool1n3sjq3qvu5vvcd6aud6ndcwq7r3ghmkafcg60gznlwfrk2ucxku";
const ADDR = "addr_test1qzyttcj6czjltcs3tn3vls6yg90542lctrzwg5aagduqlfgupztxhczzmuakzfuwvrht542yrx7ll3fk29lcl2xl8axqh3pjdh";
// 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 pool using the previously collected values.*

```typescript
const BODY = {
  changeAddress: ADDR,
  delegations: [
    {
      type: "pool",
      address: ADDR,
      keyHash: POOL_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-pool.ts
```

### Output

{% code overflow="wrap" %}

```json
{
  "hash": "a4633c716755a8d72d07c058ffabe94c9b8b0f8273a8d7babb1f607050fb3c3b",
  "complete": "84a700d90102828258203376a58b03a1a26108d79e3db28207d1e3b7db51266bb9d07691b3d724d8198201825820f4bf2eb3572ec503081a24baf506ee26f5a2ee8d5d6c707830b75516db9bb9ed020182a300581d60355efb09c4a29c7e3b63ead47a220efcbbdef1fd5369c4efff947b4b011a001dc130028201d8184a49616e76696c2d7461678258390088b5e25ac0a5f5e2115ce2cfc344415f4aabf858c4e453bd43780fa51c08966be042df3b61278e60eeba554419bdffc536517f8fa8df3f4c1a38227e99021a00032095031a053f215904d901028282008200581c1c08966be042df3b61278e60eeba554419bdffc536517f8fa8df3f4c83028200581c1c08966be042df3b61278e60eeba554419bdffc536517f8fa8df3f4c581c9c6120440ce518cc375de37536e1c0f0e28beedd4e11a7a053fb923b081a053f05390ed9010281581c355efb09c4a29c7e3b63ead47a220efcbbdef1fd5369c4efff947b4ba100d90102818258204bc7618201307ab45cdb3793107822c47230f097e9771fb358473cdb82b2e644584020ecc8e8a0b6d2f4a2bd51d388ee20abebf4982d55ce738d9b1c223bc77152fa175c15729e96073873d262017ea113ff41a8ec26a16441029f00fbf004229b07f5f6",
  "stripped": "84a700d90102828258203376a58b03a1a26108d79e3db28207d1e3b7db51266bb9d07691b3d724d8198201825820f4bf2eb3572ec503081a24baf506ee26f5a2ee8d5d6c707830b75516db9bb9ed020182a300581d60355efb09c4a29c7e3b63ead47a220efcbbdef1fd5369c4efff947b4b011a001dc130028201d8184a49616e76696c2d7461678258390088b5e25ac0a5f5e2115ce2cfc344415f4aabf858c4e453bd43780fa51c08966be042df3b61278e60eeba554419bdffc536517f8fa8df3f4c1a38227e99021a00032095031a053f215904d901028282008200581c1c08966be042df3b61278e60eeba554419bdffc536517f8fa8df3f4c83028200581c1c08966be042df3b61278e60eeba554419bdffc536517f8fa8df3f4c581c9c6120440ce518cc375de37536e1c0f0e28beedd4e11a7a053fb923b081a053f05390ed9010281581c355efb09c4a29c7e3b63ead47a220efcbbdef1fd5369c4efff947b4ba0f5f6",
  "witnessSet": "a100d90102818258204bc7618201307ab45cdb3793107822c47230f097e9771fb358473cdb82b2e644584020ecc8e8a0b6d2f4a2bd51d388ee20abebf4982d55ce738d9b1c223bc77152fa175c15729e96073873d262017ea113ff41a8ec26a16441029f00fbf004229b07"
}
```

{% endcode %}

## The Whole File (Deno Version)

<details>

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

{% code overflow="wrap" %}

```typescript
const POOL_ID = "pool1n3sjq3qvu5vvcd6aud6ndcwq7r3ghmkafcg60gznlwfrk2ucxku";
const ADDR = "addr_test1qzj4p30l8kzrm95wmfuqh4spnvpvyjt4lyc8xj2mtl2xtrsqma6nh06158nvt3w9gf2xyl35h5csz5awufc6xtvpdhszkvcml";
// See Authentication page for API key details.
const X_API_KEY = "testnet_EyrkvCWDZqjkfLSe1pxaF0hXxUcByHEhHuXIBjt9";

const BODY = {
  changeAddress: ADDR,
  delegations: [
    {
      type: "pool",
      address: ADDR,
      keyHash: POOL_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>


---

# 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/delegations/delegate-to-a-pool/deno-and-fetch.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.
