# n8n

> Remember and recall from any n8n workflow with the HTTP Request node, and give the AI Agent node what is known about each user.

Use Geniffy from any n8n workflow with the **HTTP Request** node: one node remembers what a trigger brings
in, another asks what is known, and the **AI Agent** node gets a memory of each user. Your key stays in an
n8n credential, out of the workflow itself.

## Add your key as a credential

Make a key under **API keys** in the Geniffy app and call it `n8n`. In an HTTP Request node, set:

| Parameter | Value |
| --- | --- |
| Authentication | Generic Credential Type |
| Generic Auth Type | Bearer Auth |
| Credential | A new credential, with your key as the **Bearer Token** |

Every Geniffy node in your workflows can use the same credential.

## Remember what a trigger brings in

| Parameter | Name | Value |
| --- | --- | --- |
| Method | | POST |
| URL | | `https://api.geniffy.com/v1/memories` |
| Send Body | | On |
| Body Content Type | | JSON |
| Specify Body | | Using Fields Below |
| Body Parameters | `text` | The words to remember, such as `{{ $json.text }}` |
| Body Parameters | `space` | One of your users, such as `customer_1042` (optional) |

A space name uses letters, digits, dots, dashes and underscores, so use an id rather than an email
address. Leave `space` out to use your own memory.

To fill the node in faster, choose **Import cURL** and paste the call without its key, then set the
credential as above:

```bash
curl https://api.geniffy.com/v1/memories \
  -H "Content-Type: application/json" \
  -d '{"text": "Priya Nair signs the Lumen renewal.", "space": "customer_1042"}'
```

## Ask what is known

| Parameter | Name | Value |
| --- | --- | --- |
| Method | | POST |
| URL | | `https://api.geniffy.com/v1/ask` |
| Send Body | | On |
| Body Content Type | | JSON |
| Specify Body | | Using Fields Below |
| Body Parameters | `question` | The question, such as `{{ $json.question }}` |
| Body Parameters | `space` | The same space you remember into (optional) |

The node returns `answer`, or `answer` empty and a `message` saying that nothing remembered supports one.
To branch on it, follow with an **If** node that checks `{{ $json.answer }}` **is not empty**.

## Give the AI Agent a memory

Put an HTTP Request node named `Recall` before the **AI Agent** node. It POSTs to
`https://api.geniffy.com/v1/context` with `question` set to the user's message, such as
`{{ $json.chatInput }}` from a chat trigger, and the user's `space`. Then, in the AI Agent node, add the
option **System Message**:

```text
You are a helpful assistant.

<memory>
{{ $('Recall').item.json.context }}
</memory>
```

When nothing is known, `context` says so in one sentence, so the agent says it doesn't know instead of
guessing.

To remember each exchange, add one more HTTP Request node after the agent that POSTs to
`https://api.geniffy.com/v1/memories`, with **Specify Body** set to **Using JSON**:

```text
{
  "space": "customer_1042",
  "messages": [
    { "role": "user", "content": {{ JSON.stringify($('Recall').item.json.question) }} },
    { "role": "assistant", "content": {{ JSON.stringify($json.output) }} }
  ]
}
```

`JSON.stringify` keeps quotes and line breaks in the messages from breaking the body.

Source: https://docs.geniffy.com/integrations/n8n
