How to Integrate with Dify

Learn how to use the SnackPrompt AI Engine API as an external knowledge source in Dify applications and workflows.

Overview

Dify is a platform for building LLM applications. It offers several ways to integrate external APIs:

Method
Use Case
Description

External Knowledge API

RAG replacement

Use external API instead of built-in knowledge

HTTP Request Node

Workflows

Call API in workflow nodes

Custom Tool

Agent tools

Agent decides when to query the API

API Extension

Advanced

Create reusable API integration

Integration Architecture

┌─────────────────────────────────────────────────────────┐
│                        Dify                             │
│  ┌─────────────┐    ┌─────────────┐    ┌────────────┐   │
│  │    User     │──▶│  Chatflow/  │──▶│  Response   │   │
│  │   Input     │    │  Workflow   │    │            │   │
│  └─────────────┘    └──────┬──────┘    └────────────┘   │
│                            │                            │
│                     ┌──────▼──────┐                     │
│                     │   HTTP /    │                     │
│                     │ Knowledge   │                     │
│                     └──────┬──────┘                     │
└────────────────────────────┼────────────────────────────┘


              ┌──────────────────────────────┐
              │  SnackPrompt AI Engine API   │
              │  /v1/kb/search or /v1/kb/chat│
              └──────────────────────────────┘

Dify supports connecting to external knowledge bases via API. This is the most native integration.

Step 1: Configure External Knowledge

  1. Go to Knowledge in Dify

  2. Click External Knowledge API

  3. Click Add External Knowledge API

Step 2: API Configuration

Field
Value

Name

SnackPrompt Knowledge Base

API Endpoint

https://api-integrations.snackprompt.com/v1/kb/search

API Key

Your SnackPrompt API key

Step 3: Request Configuration

Request Body Template:

Response Mapping:

Dify Field
API Response Path

records

items

content

payload.original_text

score

score

metadata.id

payload.snack_item_id

Step 4: Use in Applications

  1. Create or edit a Chatbot or Agent application

  2. In Context, select your external knowledge

  3. Configure retrieval settings (top_k, score threshold)


Method 2: HTTP Request in Workflows

Use when building complex workflows that need API calls at specific points.

Step 1: Create a Workflow

  1. Go to Studio > Create App > Workflow

  2. Add nodes to your workflow

Step 2: Add HTTP Request Node

  1. Add HTTP Request node

  2. Configure:

Field
Value

Method

POST

URL

https://api-integrations.snackprompt.com/v1/kb/search

Headers:

Body:

Step 3: Process Results

Add a Code node to format results:

Step 4: Generate Response

Add LLM node with prompt:


Method 3: Chat Endpoint for Simple Integration

Use /v1/kb/chat when you want the API to handle all RAG processing.

Workflow Structure

HTTP Request Configuration

Field
Value

URL

https://api-integrations.snackprompt.com/v1/kb/chat

Method

POST

Headers:

Body:

Extract Response

The API returns:

  • answer: Ready-to-use response

  • sources: Source documents used

Use in template: {{#http_request.answer#}}


Method 4: Custom Tool for Agents

Create a custom tool that agents can use to search your knowledge base.

Step 1: Go to Tools

  1. Navigate to Tools in Dify

  2. Click Create Custom Tool

Step 2: Configure Tool

Basic Information:

Field
Value

Name

search_knowledge_base

Description

Search the company knowledge base for information about products, policies, and procedures. Use when you need factual information to answer user questions.

Parameters:

Name
Type
Required
Description

query

string

Yes

The search query

tags

array

No

Filter by specific tags

limit

number

No

Maximum results (default: 5)

Step 3: Tool Schema

Step 4: Authentication

Configure API key authentication:

Field
Value

Auth Type

API Key

Header Name

x-api-key

API Key

Your SnackPrompt API key

Step 5: Use in Agent

  1. Create an Agent application

  2. Add your custom tool to the agent's toolset

  3. Configure the agent's system prompt to use the tool


Practical Use Cases

1. Simple Q&A Chatbot

Native integration using External Knowledge API.

2. Support Agent with Tools

Agent that can search and take actions.

3. RAG Workflow with Validation

4. Multi-Source Knowledge

5. Conversational RAG with Memory


Configuration Tips

1. Optimize Retrieval Settings

In your chatbot/agent configuration:

Setting
Recommended Value

Top K

3-5

Score Threshold

0.5

Reranking

Enable if available

2. Tool Description Matters

For agents, write detailed tool descriptions:

3. Handle Empty Results

In Code nodes:

4. Use Variables for Configuration

Store configuration in Dify variables:

Variable
Value

snackprompt_tenant_id

Your tenant ID

snackprompt_default_limit

5

Reference in HTTP body:

5. Filter by Context

Use tags to narrow search scope:


Complete Example: Customer Service Chatbot

Application Type

Agent with tools

System Prompt

Tools Configuration

Tool 1: search_knowledge_base

  • Endpoint: /v1/kb/search

  • Use for: General knowledge queries

Tool 2: search_products

  • Endpoint: /v1/kb/search with tag_names: ["Products"]

  • Use for: Product-specific queries

Conversation Opener


Troubleshooting

Error: "tenant_id is required"

Ensure tenant_id is inside the filters object:

External Knowledge not returning results

  1. Verify the API endpoint URL

  2. Check API key is correct

  3. Test API directly with curl/Postman

  4. Verify response mapping matches API response structure

Agent doesn't use the tool

  1. Improve tool description

  2. Add explicit instructions in system prompt

  3. Test with queries like "search the knowledge base for..."

  4. Check tool is enabled in agent settings

Workflow HTTP request fails

  1. Check URL is correct

  2. Verify headers are properly formatted

  3. Check body JSON is valid

  4. Look at error response for details

Slow response times

  1. Reduce limit parameter

  2. Add caching if supported

  3. Check network latency to API

  4. Consider using /chat endpoint for simpler flows


API Response Mapping Reference

Search Endpoint Response

Mapping for External Knowledge

Dify Expects
SnackPrompt Returns
Mapping

records

items

Direct

content

payload.original_text

Nested

score

score

Direct

title

payload.snack_item_id

Or custom

Chat Endpoint Response


External Resources

Last updated

Was this helpful?