Dify Plugin SDK
The Dify Plugin SDK allows developers to build custom plugins for the Dify platform. It provides classes and decorators to define plugin metadata, inputs, outputs, and API endpoints, enabling seamless integration with Dify AI applications. Currently at version 0.7.4, the library is under active development with frequent releases.
Warnings
- breaking The library is under rapid development. API changes, including breaking ones, may occur frequently between minor versions (e.g., 0.x.x to 0.y.x) as the project matures.
- gotcha Plugins require Python 3.11 or higher. Using older Python versions will result in installation failures or runtime errors.
- gotcha To expose functionality, plugin methods must be decorated with `@plugin.api("endpoint_name")`. Forgetting this decorator or using an incorrect endpoint name will prevent Dify from discovering and invoking your plugin's capabilities.
- gotcha The `PluginMetadata` object requires essential fields like `name`, `description`, `icon`, `category`, `inputs`, and `outputs` to be correctly and completely defined. Incomplete or incorrect metadata can lead to the plugin not being recognized or functioning improperly within Dify.
Install
-
pip install dify-plugin
Imports
- DifyPlugin
from dify_plugin import DifyPlugin
- PluginMetadata
from dify_plugin import PluginMetadata
- Category
from dify_plugin import Category
- Input
from dify_plugin import Input
- Output
from dify_plugin import Output
- HttpAPI
from dify_plugin import HttpAPI
- Markdown
from dify_plugin import Markdown
Quickstart
import os
from dify_plugin import DifyPlugin, PluginMetadata, Category, Input, Output
plugin = DifyPlugin(
metadata=PluginMetadata(
name="Hello Dify",
description="A simple plugin that says hello.",
icon="https://dify.ai/favicon.ico",
category=Category.TOOLS,
inputs=[
Input(name="name", type="string", required=True, description="Your name"),
],
outputs=[
Output(name="greeting", type="string"),
],
)
)
@plugin.api("hello")
def hello_world_api(name: str):
return {"greeting": f"Hello, {name}!"}
if __name__ == "__main__":
# In a real Dify environment, the plugin would be served externally.
# For local testing, run this script and access at http://127.0.0.1:PORT/.well-known/ai-plugin.json
plugin.run()