# Delegations

## API Overview

Delegations in Cardano enable users to participate in the network without running infrastructure themselves. Stake pool delegations allow users to earn rewards by supporting the network's security, while DRep delegations empower users to participate in governance decisions. The Anvil API simplifies implementing both delegation types through a unified interface, letting applications offer these essential features without complex blockchain interactions.

| Type   | Purpose                            | Certificate Created |
| ------ | ---------------------------------- | ------------------- |
| `pool` | Delegate to stake pool for rewards | `StakeDelegation`   |
| `drep` | Delegate to DRep for governance    | `VoteDelegation`    |

## API Endpoint

**URL**: `https://preprod.api.ada-anvil.app/v2/services/transactions/build`\
**Method**: POST\
**Headers**: `Content-Type: application/json`, `x-api-key: YOUR_API_KEY`

## Request Format

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

## Parameters

| Parameter       | Description                       | Required            |
| --------------- | --------------------------------- | ------------------- |
| `type`          | Either `"pool"` or `"drep"`       | Yes                 |
| `address`       | User's full address               | Yes                 |
| `keyHash`       | Pool ID or DRep ID                | Yes                 |
| `changeAddress` | Address for change                | Yes                 |
| `utxos`         | Array of UTXOs in CBOR hex format | Yes (in production) |

## Implementation Guides

{% content-ref url="/pages/9N5ko1j3NXCrFh8NoX5C" %}
[Delegate to a DRep](/guides/delegations/delegate-to-a-drep.md)
{% endcontent-ref %}

{% content-ref url="/pages/nph9znb6kA1L9wLPrwd0" %}
[Delegate to a Stake Pool](/guides/delegations/delegate-to-a-pool.md)
{% endcontent-ref %}

## Transaction Workflow

1. **Prepare**: Include delegation certificates in transaction
2. **Build & Sign**: Process transaction with proper certificates and signatures
3. **Submit**: Send to network (changes effective after current epoch)

## Best Practices

* **Verification**: Confirm pool/DRep IDs before delegation
* **Testing**: Use testnet before mainnet operations
* **Error Handling**: Implement robust handling for:
  * Invalid address formats
  * Unknown pool/DRep IDs
  * Insufficient funds
  * Missing signers

## Related Resources

* [Signing Transactions](https://github.com/Cardano-Forge/anvil-api/blob/main/docs/guides/transaction/signing-transaction.md)
* [Submitting Transactions](/guides/transaction/submit-transaction.md)


---

# 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.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.
