# Building an Autonomous AI Agent

## **Integrating raia AI Agents with n8n Workflows via API & Webhooks**

### 🧭 Overview

You can connect **raia AI Agents** with **n8n**, a powerful low-code automation engine, to trigger and automate workflows based on CRM events, form submissions, or any business logic.

This integration allows you to:

* Trigger an AI conversation via **API**
* Have the agent engage via **SMS, Email, or Voice**
* Capture the output (transcript, score, summary)
* Send it back to **n8n** via **Webhook** for CRM updates or downstream automation

***

### 🧠 Example: CRM → AI Agent → Prospect → CRM

#### Goal:

When a **lead’s status is updated in your CRM**, an n8n workflow sends lead details to a **Sales AI Agent** in raia.\
The AI agent:

1. Starts a conversation (via SMS, Email, or Voice)
2. Qualifies the lead
3. Scores and summarizes the interaction
4. Sends the results to an n8n webhook
5. n8n updates the lead record in your CRM

***

### 🔗 Architecture Flow

```
CRM → (status change) → n8n Workflow #1
        ↓
  API call to raia → Start conversation
        ↓
   AI Agent handles chat via SMS/email/voice
        ↓
 Conversation ends → Summary + Score generated
        ↓
  raia sends to Webhook (n8n Workflow #2)
        ↓
  CRM updated with results
```

***

### ⚙️ Step-by-Step Integration

#### 🧱 Step 1: Create and Train the Agent

* Use **Launch Pad** to create a **Sales Agent**
* Train it on:
  * Qualification criteria
  * Product/service info
  * Rebuttal and objection handling
* Enable:
  * **SMS / Email / Voice** skills
  * **Scoring Skill** (auto-qualify)
  * **Webhook Skill** (post back to n8n)
  * **Notification Skill** (optional real-time alerts)

***

#### 🔐 Step 2: Start the Conversation via API

From your **n8n workflow**, send a POST request to raia:

**Endpoint**

```
POST https://api.raia2.com/external/conversations/start
```

**Headers**

```http
Agent-Secret-Key: YOUR_API_KEY
Content-Type: application/json
Accept: */*
```

**Payload Example**

```json
{
  "firstName": "John",
  "lastName": "Doe",
  "context": "Sales",
  "conversationContext": "Follow-Up",
  "source": "crm",
  "fkId": "lead-4567",
  "fkUserId": "rep-1234",
  "customData": {
    "campaign": "Q3 Promo",
    "industry": "ecommerce"
  },
  "channel": "sms",
  "email": "john.doe@example.com",
  "emailSubject": "Let’s Connect",
  "emailIntroduction": "Hi John, just following up...",
  "includeSignatureInEmail": false,
  "phoneNumber": "+1234567890",
  "resetConversation": false
}
```

> 📌 Set `channel` to `"sms"`, `"email"`, or `"voice"` depending on how you want the agent to reach out.\
> 🔁 Use `customData` to pass any relevant CRM fields.\
> 🧠 `fkId` and `fkUserId` let you correlate conversations with internal records.

***

#### 🌐 Step 3: Configure Webhook Skill in raia

In the agent’s **Skills → Webhook**:

* Set **Webhook URL** to an **n8n Webhook Node**
* Trigger: `OnThreadEnd`
* Payload includes:
  * Conversation transcript
  * Summary
  * Score
  * Contact metadata

***

### 🔁 n8n Workflow #1: Trigger the Agent

**Trigger**:

* CRM event (webhook, polling, or integration node)

**Steps**:

1. Extract contact fields (name, phone, email)
2. Format payload for raia
3. Send **HTTP POST** to `https://api.raia2.com/external/conversations/start`
4. (Optional) Log status or notify Slack

***

### 🔄 n8n Workflow #2: Receive & Handle AI Response

**Trigger**:

* **Webhook Node** receives POST from raia

**Steps**:

1. Parse JSON payload
2. Extract:
   * Score
   * Summary
   * Full transcript
3. Update CRM:
   * Set status = “Contacted” or “Qualified”
   * Log summary and transcript as notes
   * Tag or assign rep
4. (Optional) Alert team via email or Slack

***

### 🧠 Example n8n Workflow Designs

#### Workflow #1: Trigger AI Agent

```plaintext
[CRM Trigger] → [Set Variables] → [HTTP Request to raia] → [Log or Notify]
```

#### Workflow #2: Handle AI Summary

```plaintext
[Webhook Trigger] → [Parse JSON] → [CRM Update] → [Notify Sales]
```

***

### 🧰 Tools Involved

| Component         | Tool/Service              |
| ----------------- | ------------------------- |
| AI Agent Platform | raia (Launch Pad, Skills) |
| Workflow Engine   | n8n                       |
| Messaging Channel | SMS, Email, or Voice      |
| CRM Integration   | Any (via n8n connectors)  |
| Data Transfer     | raia API + Webhook        |


---

# 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://docs.raiaai.com/dev-program/agents/building-an-autonomous-ai-agent.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.
