Skip to content

Add a page about the HTTP integration #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"group": "ENTERPRISE CONNECTIONS",
"pages": [
"integration/portals",
"integration/mcp"
"integration/mcp",
"integration/http"
]
},
{
Expand Down
Binary file added http.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/http.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions integration/http.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
title: "HTTP API"
description: "Integrate your agents to the rest of your system with HTTP calls"
---


![Http](/http.png)

An agent node can provide an HTTP endpoint to access agents:

- List all agents on the node.
- Start a conversation with an agent.
- List conversations.
- List all the tools on the node.

You can also easily create our own HTTP routes and tailor the interactions with your agent to your needs.

Here is how you can start a standard HTTP server when creating an agent node:

```python images/main/main.py
from ockam import Agent, Node, Repl, HttpServer
from sys import argv


async def main(node):
agent = await Agent.start(node,
name="cerberus",
instructions="You use network tools to detect intrusions")
await Repl.start(agent, argv[1])


Node.start(main, http_server=HttpServer(listen_address=argv[2]))
```

This will serve the node standard HTTP API to interact with agents. For example:

```bash
# return the list of all agents referenced on the node (either local or remote)
curl http://127.0.0.1:8000/agents

# send a message to an agent
curl -X POST http://localhost:8000/agents/cerberus \
-H "Content-Type: application/json" \
-d '{"message":"Is my network safe?", "scope":"acme", "conversation":"1"}'

# retrieve all the conversations for a given scope
curl http://localhost:8000/agents/cerberus/scopes/acme
```

You can also define your own API, by creating your own routes:

```python images/main/api.py
from fastapi import FastAPI
from fastapi.responses import JSONResponse

class Api:
def __init__(self):
self.api = FastAPI()

def routes(self, node):
@self.api.post("/analysis")
async def create_analysis(network: str):
print(f"Analyzing the network {network}...")
return JSONResponse(content={"status": "ok"})
```

The access to `node` gives you the mean to interact with all the agents referenced on the node and even start new ones if necessary!

Finally the `Api` is set on the `HttpServer` to ensure it is merged with the rest of the node API:
```python images/main/main.py
from ockam import Agent, Node, Repl, HttpServer
from sys import argv
from api import Api

async def main(node):
agent = await Agent.start(node,
name="cerberus",
instructions="You use network tools to detect intrusions")
await Repl.start(agent, argv[1])


Node.start(main, http_server=HttpServer(listen_address=argv[2], api=Api()))
```