> ## Documentation Index
> Fetch the complete documentation index at: https://autonomy.computer/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Coding Agents: How to create custom APIs for Autonomy apps and agents

> Create and deploy custom HTTP APIs for Autonomy apps and agents using FastAPI. This guide covers covers custom APIs, endpoint design, parallel processing, and streaming responses.

# A guide for coding agents to create and deploy custom APIs for Autonomy apps and agents

Use the **Autonomy Framework** and FastAPI to add custom HTTP APIs to Autonomy apps and deploy them on **Autonomy Computer**.

> **Key Terms**: See the [definitions section of the main guide](https://autonomy.computer/docs/_for-coding-agents.md#definitions).

*Keywords: fastapi, custom api, parallel agents, http server, endpoints*

This guide shows how to create custom FastAPI routes and build scalable multi-agent architectures.

The **Autonomy Framework** provides built-in APIs for Agents. See the [guide on creating and deploying a new Autonomy app](https://autonomy.computer/docs/_for-coding-agents/create-a-new-autonomy-app.md) for examples of how to use the built-in API endpoints.

## Define a custom API

If the app needs more control, instead of using the built-in agent endpoints, the app can have custom FastAPI routes that are tailored to you application.

**Important:** Don't define `@app.get("/")` in your FastAPI app if you want the Autonomy HTTP server to automatically serve your `index.html` file at the root URL. Custom root routes take precedence over automatic file serving.

Here is an example:

### Create a simple translation API

In a new Autonomy app, create a `images/main/main.py` that defines a simple translation service:

```python theme={null}
from autonomy import Agent, HttpServer, Model, Node, NodeDep
from fastapi import FastAPI

app = FastAPI()
agent = None

@app.post("/translate")
async def translate(request: dict, node: NodeDep):
    global agent

    if not agent:
        agent = await Agent.start(
            node=node,
            name="translator",
            model=Model("claude-sonnet-4-v1"),
            instructions="""
            You are an agent that specializes in translating text
            from English to Hindi. When you're given text in English,
            output the corresponding translation in Hindi written
            using the Latin alphabet (Roman script).

            Provide only the translation, no additional explanation.
            """,
        )

    response = await agent.send(f"English:\n\n{request.get("text", "")}")
    return {"translation": response[-1].content.text}

Node.start(http_server=HttpServer(app=app))
```

This creates a simple translation service that uses a single agent for all requests. The `autonomy.yaml`, `Dockerfile` remain same as in the previous section.

The `index.html` would have to be adapted to call the `/translate` endpoint. To start, we can test using curl.

### Deploy the translation API

Deploy the zone using the same command as before:

```bash theme={null}
autonomy zone deploy
```

Once, the zone is deployed, the translation API will be available at: `https://${CLUSTER}-${ZONE}.cluster.autonomy.computer/translate`

### Test the translation API

Test your translation service by sending English text to be translated:

```bash theme={null}
timeout 15s curl --silent --request POST \
--header "Content-Type: application/json" \
--data '{
  "text": "Hello, how are you?"
}' \
"https://${CLUSTER}-${ZONE}.cluster.autonomy.computer/translate"
```

Expected response:

```json theme={null}
{
  "translation": "Namaste, aap kaise hain?"
}
```

## Parallelize work using many agents

### Create a parallel translation API

For more advanced use cases, the app can process multiple translations concurrently by running thousands of parallel agents.

Replace `images/main/main.py` with:

```python theme={null}
from autonomy import Agent, HttpServer, Model, Node, NodeDep
from fastapi import FastAPI
from asyncio import gather, create_task
from typing import List, Dict

app = FastAPI()

async def translate_item(node: Node, item: str) -> Dict[str, str]:
    agent = None
    try:
        agent = await Agent.start(
            node=node,
            name=f"translator_{id(item)}",
            instructions="""
            You are an agent that specializes in translating text
            from English to Hindi. When you're given text in English,
            output the corresponding translation in Hindi written
            using the Latin alphabet (Roman script).

            Provide only the translation, no additional explanation.
            """,
            model=Model("claude-sonnet-4-v1"),
        )

        response = await agent.send(f"English:\n\n{item}", timeout=60)
        return {"text": item, "translation": response[-1].content.text}
    except Exception as e:
        return {"text": item, "error": str(e)}
    finally:
        if agent:
            create_task(Agent.stop(node, agent.name))

@app.post("/translate")
async def translate(request: dict, node: NodeDep):
    texts = request.get("texts", [])
    results = await gather(*(translate_item(node, text) for text in texts))
    return {"translations": results}

Node.start(http_server=HttpServer(app=app))
```

This creates a scalable translation service that can translate thousands of items in parallel by creating an individual agent per item.

The `autonomy.yaml` and `Dockerfile` remain the same as in the [getting started guide](https://autonomy.computer/docs/_for-coding-agents/create-a-new-autonomy-app.md). The `index.html` would have to be adapted to call the `/translate` endpoint. To start, we can test using curl.

### Deploy the parallel translation API

Deploy the zone:

```bash theme={null}
autonomy zone deploy
```

### Test the parallel translation API

Test the parallel translation API by sending multiple English phrases:

```bash theme={null}
timeout 15s curl --silent --request POST \
--header "Content-Type: application/json" \
--data '{
  "texts": [
    "Hello, how are you?",
    "Thank you very much",
    "Good morning"
  ]
}' \
"https://${CLUSTER}-${ZONE}.cluster.autonomy.computer/translate"
```

Expected response format:

```json theme={null}
{
  "translations": [
    {
      "text": "Hello, how are you?",
      "translation": "Namaste, aap kaise hain?"
    },
    {
      "text": "Thank you very much",
      "translation": "Dhanyawad bahut bahut"
    },
    {
      "text": "Good morning",
      "translation": "Suprabhat"
    }
  ]
}
```

## View your API documentation

FastAPI automatically generates interactive API documentation. View it at: `https://${CLUSTER}-${ZONE}.cluster.autonomy.computer/docs`

This provides a complete interface to test your endpoints directly in the browser.

## Related Guides

* [How to create and deploy a new Autonomy app](https://autonomy.computer/docs/_for-coding-agents/create-a-new-autonomy-app.md)
* [How to create custom user interfaces](https://autonomy.computer/docs/_for-coding-agents/create-custom-ui.md)
* [How to use memory, conversation, and scope in Autonomy agents](https://autonomy.computer/docs/_for-coding-agents/memory.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.