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:
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│
└──────────────────────────────┘Method 1: External Knowledge API (Recommended)
Dify supports connecting to external knowledge bases via API. This is the most native integration.
Step 1: Configure External Knowledge
Go to Knowledge in Dify
Click External Knowledge API
Click Add External Knowledge API
Step 2: API Configuration
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:
records
items
content
payload.original_text
score
score
metadata.id
payload.snack_item_id
Step 4: Use in Applications
Create or edit a Chatbot or Agent application
In Context, select your external knowledge
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
Go to Studio > Create App > Workflow
Add nodes to your workflow
Step 2: Add HTTP Request Node
Add HTTP Request node
Configure:
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
URL
https://api-integrations.snackprompt.com/v1/kb/chat
Method
POST
Headers:
Body:
Extract Response
The API returns:
answer: Ready-to-use responsesources: 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
Navigate to Tools in Dify
Click Create Custom Tool
Step 2: Configure Tool
Basic Information:
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:
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:
Auth Type
API Key
Header Name
x-api-key
API Key
Your SnackPrompt API key
Step 5: Use in Agent
Create an Agent application
Add your custom tool to the agent's toolset
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:
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:
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/searchUse for: General knowledge queries
Tool 2: search_products
Endpoint:
/v1/kb/searchwithtag_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
Verify the API endpoint URL
Check API key is correct
Test API directly with curl/Postman
Verify response mapping matches API response structure
Agent doesn't use the tool
Improve tool description
Add explicit instructions in system prompt
Test with queries like "search the knowledge base for..."
Check tool is enabled in agent settings
Workflow HTTP request fails
Check URL is correct
Verify headers are properly formatted
Check body JSON is valid
Look at error response for details
Slow response times
Reduce
limitparameterAdd caching if supported
Check network latency to API
Consider using
/chatendpoint for simpler flows
API Response Mapping Reference
Search Endpoint Response
Mapping for External Knowledge
records
items
Direct
content
payload.original_text
Nested
score
score
Direct
title
payload.snack_item_id
Or custom
Chat Endpoint Response
Related
External Resources
Last updated
Was this helpful?