# n8n

Using SurrealDB and SurrealDB Agent Memory from n8n workflows.

[n8n](https://n8n.io) is a workflow-automation tool. The official community node **`@surrealdb/n8n-nodes-surrealdb`** connects a workflow to SurrealDB for full CRUD and custom SurrealQL, and works as both an action node and an [AI agent tool node](https://docs.n8n.io/advanced-ai/). To give an n8n AI Agent long-term memory with SurrealDB Agent Memory, call the [REST API](/docs/agent-memory/integrations/surfaces/rest.md) from an **HTTP Request** node.

## Install the SurrealDB node

The node runs on **self-hosted** n8n (v0.214.0+):

1. Open **Settings → Community Nodes**.
2. Click **Install** and enter `@surrealdb/n8n-nodes-surrealdb`.
3. Restart n8n if prompted.

It requires SurrealDB v3.0.0+ and connects over HTTP/HTTPS (WebSocket is not supported). It offers record, table, field, index, and relationship operations, a visual `SELECT` builder, and raw SurrealQL execution.

## Give an AI agent memory with SurrealDB Agent Memory

SurrealDB Agent Memory is a hosted service reached over HTTP with a Bearer key, so an **HTTP Request** node is the direct path. Store the endpoint and API key in n8n **Credentials** (Header Auth), not in the node.

**Recall before the agent runs** with an HTTP Request node calling `context`:

```
POST https://api.spectron.example/api/v1/acme-prod/context
Authorization: Bearer <your-api-key>
Content-Type: application/json

{
  "query": "{{ $json.chatInput }}",
  "scope": ["org/acme/user/{{ $json.userId }}"],
  "k": 8
}
```

Feed the returned context block into the AI Agent node's system prompt.

**Store the turn afterwards** with an HTTP Request node calling `facts/batch`:

```
POST https://api.spectron.example/api/v1/acme-prod/facts/batch
Authorization: Bearer <your-api-key>
Content-Type: application/json

{
  "turns": [
    { "role": "user", "content": "{{ $json.chatInput }}" },
    { "role": "assistant", "content": "{{ $json.output }}" }
  ],
  "scope": ["org/acme/user/{{ $json.userId }}"]
}
```

## Expose Agent Memory as an agent tool

Wrap either request in an **HTTP Request Tool** node and attach it to the AI Agent. The agent then calls recall or remember on its own during a run, the same way the framework adapters expose SurrealDB Agent Memory tools.

## Scope per user or workflow

Set the `scope` on each call to isolate memory. A scope is a slash path or an array of paths, for example `["org/acme/user/alice"]`. Derive it from workflow data such as a chat user id. Register paths with `spectron scopes create` before first use.

## Next steps

- [REST API](/docs/agent-memory/integrations/surfaces/rest.md): the endpoints the HTTP Request nodes call
- [MCP server](/docs/agent-memory/integrations/mcp-server/install.md): if your n8n host speaks MCP instead
