MCPcopy Index your code
hub / github.com/chat-pass/dify-api-java-sdk

github.com/chat-pass/dify-api-java-sdk @v1.0.2.FINAL

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.2.FINAL ↗ · + Follow
676 symbols 2,308 edges 157 files 253 documented · 37%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Here's the English version of the README for the Dify API Java SDK:

Dify API Java SDK

Maven Central License Java codecov

English | 简体中文) | 日本語

The Dify API Java Client is a Java client library for interacting with the Dify platform. It provides full support for Dify's application API and knowledge base API, allowing Java developers to easily integrate Dify's generative AI capabilities into their applications.

Features

The Dify API Java Client offers the following core features:

1. Support for Multiple Application Types

  • Conversational Applications (Chat): Use DifyChatApi to call conversational applications, supporting session management, message feedback, and more.
  • Text Generation Applications (Completion): Use DIfyCompletionApi to call text generation applications.
  • Workflow Orchestrated Conversations (Chatflow): Use DifyChayflowApi to call workflow orchestrated conversational applications.
  • Workflow Applications (Workflow): Use DifyWorkflowApi to call workflow applications.
  • Knowledge Base Management (Datasets): Use DifyDatasetsApi to manage knowledge bases, documents, and retrieval.

2. Rich Interaction Modes

  • Blocking Mode: Synchronous API calls, waiting for a complete response.
  • Streaming Mode: Receive real-time generated content via callbacks, supporting typewriter effects.
  • File Handling: Support for file uploads, speech-to-text, text-to-speech, and other multimedia functions.

3. Comprehensive Session Management

  • Create and manage sessions.
  • Retrieve historical messages.
  • Rename sessions.
  • Message feedback (like/dislike).
  • Get suggested questions.

4. Full Knowledge Base Support

  • Create and manage knowledge bases.
  • Upload and manage documents.
  • Document segmentation management.
  • Semantic retrieval.

5. Annotation Management

  • Create annotations.
  • Update annotations.
  • Delete annotations.
  • Initial setup for annotation replies.
  • Query task status for initial setup of annotation replies.

6. Flexible Configuration Options

  • Custom connection timeout.
  • Custom read/write timeout.
  • HTTP proxy support.

Installation

System Requirements

  • Java 8 or higher.
  • Maven 3.x or higher.

Maven

<dependency>
    <groupId>io.github.chat-pass</groupId>
    <artifactId>dify-api-java-sdk</artifactId>
    <version>1.0.1.FINAL</version>
</dependency>

Quick Start

Basic Usage Example

// Environment configuration
Map<String,String> envs = System.getenv();
DifyChatApi difyChatApi = DifyApiFactory.newInstance(envs.get("DIFY_BASE_URL"),envs.get("DIFY_API_KEY")).newDifyChatApi();

// Send chat message
ChatMessageRequest request = new ChatMessageRequest();
request.setUser("test-user");
request.setQuery("Hello, please introduce yourself");
request.setResponseMode(ResponseMode.BLOCKING);

ChatMessageResponse response = difyChatApi.sendChatMessage(request);
System.out.println(response.getAnswer());

📚 Complete Examples Documentation

This SDK provides rich functional examples covering all major use cases:

🔗 View Complete Examples Documentation →

The examples documentation includes: - 🚀 Basic API Examples - Application info retrieval, parameter configuration - 💬 Conversational Applications - Blocking and streaming chat, message handling
- ⚡ Workflow Applications - Execution, monitoring, log management - 📊 Dataset Management - Knowledge base creation, document management, semantic retrieval - 🎵 Audio Processing - Text-to-speech, speech-to-text - 📁 File Management - File upload, processing - 🛠️ Best Practices - Error handling, performance optimization, concurrent processing

API Reference

Client Types

Client Type Description Main Features
DifyBaseApi General base API for Dify Get application info, file upload/download, etc.
DifyChatApi Conversational application client Conversations, session management, message feedback
DifyCompletionApi Text generation application client Text generation, stop generation
DifyChatFlowApi Workflow orchestrated conversation client Workflow orchestrated conversations
DifyWorkflowApi Workflow application client Execute workflows, workflow management
DifyDatasetsApi Knowledge base client Knowledge base management, document management, retrieval

Response Modes

Mode Enum Value Description
Blocking ResponseMode.BLOCKING Synchronous call, waiting for a complete response
Streaming ResponseMode.STREAMING Receive real-time generated content via callbacks

Event Types

Event Type Description
MessageEvent Message event, includes generated text fragments
MessageEndEvent Message end event, includes complete message ID
MessageFileEvent File message event, includes file information
TtsMessageEvent Text-to-speech event
TtsMessageEndEvent Text-to-speech end event
MessageReplaceEvent Message replace event
AgentMessageEvent Agent message event
AgentThoughtEvent Agent thought event
WorkflowStartedEvent Workflow started event
NodeStartedEvent Node started event
NodeFinishedEvent Node finished event
WorkflowFinishedEvent Workflow finished event
ErrorEvent Error event
PingEvent Ping event

Advanced Configuration

Custom HTTP Client

// Create custom configuration
OkHttpClient customClient = new OkHttpClient.Builder()
                .callTimeout(30, TimeUnit.SECONDS)
                .pingInterval(15, TimeUnit.SECONDS)
                .build();
DIfyApiServiceGenerator.configureHttpClient(customClient);

HTTP Proxy Settings

DIfyApiServiceGenerator.setHttpProxy("proxy.example.com", 8080, "username", "password");

Contribution

Contributions, issue reports, and suggestions for improvements are welcome. Please participate in project development through GitHub Issues or Pull Requests.

License

This project is licensed under the Apache License 2.0.

Related Links

Extension points exported contracts — how you extend this code

DifyDatasetsApiService (Interface)
知识库 API [1 implementers]
src/main/java/io/github/chatpass/dify/service/DifyDatasetsApiService.java
TestService (Interface)
(no doc)
src/test/java/io/github/chatpass/dify/DIfyApiServiceGeneratorTest.java
DifyConversationApiService (Interface)
会话管理接口 [1 implementers]
src/main/java/io/github/chatpass/dify/service/DifyConversationApiService.java
DifyMessageSuggestedApiService (Interface)
(no doc) [3 implementers]
src/main/java/io/github/chatpass/dify/service/DifyMessageSuggestedApiService.java
DifyCompletionApiService (Interface)
文本生成 [1 implementers]
src/main/java/io/github/chatpass/dify/service/DifyCompletionApiService.java
ChatflowStreamCallback (Interface)
工作流编排对话型应用流式回调接口 [1 implementers]
src/main/java/io/github/chatpass/dify/api/callback/ChatflowStreamCallback.java

Core symbols most depended-on inside this repo

validateNonBlank
called by 133
src/main/java/io/github/chatpass/dify/util/ValidationUtils.java
executeSync
called by 87
src/main/java/io/github/chatpass/dify/DIfyApiServiceGenerator.java
build
called by 68
src/main/java/io/github/chatpass/dify/config/DifyApiConfig.java
builder
called by 60
src/main/java/io/github/chatpass/dify/config/DifyApiConfig.java
validateNonNull
called by 59
src/main/java/io/github/chatpass/dify/util/ValidationUtils.java
handleEvent
called by 40
src/main/java/io/github/chatpass/dify/api/callback/StreamEventDispatcher.java
toJson
called by 26
src/main/java/io/github/chatpass/dify/util/JSONUtils.java
newInstance
called by 14
src/main/java/io/github/chatpass/dify/DifyApiFactory.java

Shape

Method 450
Class 200
Interface 19
Enum 7

Languages

Java100%

Modules by API surface

src/main/java/io/github/chatpass/dify/api/DifyDatasetsApi.java38 symbols
src/main/java/io/github/chatpass/dify/service/DifyDatasetsApiService.java37 symbols
src/test/java/io/github/chatpass/dify/example/DifyDatasetsApiExample.java34 symbols
src/test/java/io/github/chatpass/dify/api/DifyDatasetsApiTest.java22 symbols
src/main/java/io/github/chatpass/dify/config/DifyApiConfig.java22 symbols
src/main/java/io/github/chatpass/dify/api/DifyChatFlowApi.java22 symbols
src/main/java/io/github/chatpass/dify/util/MetricsUtils.java21 symbols
src/test/java/io/github/chatpass/dify/api/DifyChatFlowApiTest.java20 symbols
src/main/java/io/github/chatpass/dify/DIfyApiServiceGenerator.java16 symbols
src/main/java/io/github/chatpass/dify/api/DifyCompletionApi.java15 symbols
src/main/java/io/github/chatpass/dify/api/DifyChatApi.java15 symbols
src/test/java/io/github/chatpass/dify/example/DifySendChatStreamMessageExample.java13 symbols

For agents

$ claude mcp add dify-api-java-sdk \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact