Dify Plugin Development: Hello World Guide
This document provides a detailed tutorial for Dify plugin development from scratch, using the Telegraph publishing plugin as an example, covering environment preparation, project initialization, virtual environment configuration, plugin core logic development, local debugging, plugin metadata enhancement, and packaging for release.
Welcome to the beginner’s guide for Dify plugin development! This tutorial assumes you have basic programming knowledge and experience using the Dify platform. After completing this tutorial, you will master the fundamental process of creating a simple Dify plugin, taking you from a Dify user to a plugin contributor.
We will demonstrate step by step how to develop a simple Telegraph publishing plugin. This tutorial primarily uses command line operations, with examples that use VSCode as the code editor. We recommend first reading through the entire tutorial to understand the overall process, then following the steps to work hands-on.
Note for LLMs: You can also use our built prompt to assist you in developing plugins: Dify Plugin Development: Prompt
Before you begin, you can always refer to our Developer Cheatsheet for common commands and information, or consult the complete developer documentation when facing more complex issues.
1. Preparing the Development Environment
Before starting Dify plugin development, ensure you have the following tools ready in your environment:
- Dify Plugin Development Scaffold (CLI): This is the core tool for developing, debugging, and packaging plugins, also known as
dify-plugin-daemon
or “Plugin Development SDK.” - Python Environment: Python 3.12 or higher is required.
1.1 Installing the Dify Plugin Development Scaffold (CLI)
For a more detailed guide on preparing the development environment, please refer to Initializing Development Tools.
-
Download: Visit the Dify Plugin CLI Releases page. Download the latest version of the binary file corresponding to your operating system (Windows, macOS Intel/ARM, Linux).
-
Set Execute Permissions (macOS / Linux):
-
The following steps use macOS (Apple Silicon / M series chips) as an example, assuming the downloaded file is named
dify-plugin-darwin-arm64
. In the terminal, navigate to the directory containing the file and execute the following command to grant execution permissions: -
For Linux users, download the corresponding Linux version file and execute a similar command like
chmod +x <downloaded_filename>
. -
For Windows users, after downloading the
.exe
file, you can typically run it directly.
-
-
Verify Installation:
-
In the terminal, execute the following command to check if the tool runs properly (replace
./dify-plugin-darwin-arm64
with the actual filename or path you downloaded): -
If the terminal successfully outputs version information (e.g.,
v0.0.1-beta.15
), then the installation is successful.
-
Tips:
- macOS Security Prompt: If macOS initially prompts “Apple cannot verify” or “Cannot open,” go to “System Settings” → “Privacy & Security” → “Security” section, find the related prompt and click “Open Anyway” or “Allow.”
- Simplify Command: You can rename the downloaded binary file to a shorter name (e.g.,
dify
ordify-plugin
) for easier use. Example:mv dify-plugin-darwin-arm64 dify
, then you can use./dify version
.- Global Installation (Optional): If you want to run the command from any path in your system (e.g., directly typing
dify
instead of./dify
), you can move the renamed file to a directory included in your system’sPATH
environment variable, such as/usr/local/bin
(macOS/Linux) or add it to Windows environment variables.
- For example (macOS/Linux):
sudo mv dify /usr/local/bin/
- After configuration, typing
dify version
directly in the terminal should successfully output the version number.
For convenience, this article will use ./dify
as an example command for the Dify plugin development scaffold. Please replace it with your corresponding command based on your actual situation.
2. Initializing the Plugin Project
Now, let’s use the scaffold tool to create a new plugin project.
-
Open a terminal and execute the initialization command:
-
Enter the basic information for the plugin according to the prompts:
- Plugin name: A unique identifier for the plugin. For example:
telegraph
- Constraints: 1-128 characters, can only contain lowercase letters, numbers, hyphens (-), and underscores ()._
- Author: The identifier for the plugin author. For example:
alterxyz
- Constraints: 1-64 characters, can only contain lowercase letters, numbers, hyphens (-), and underscores ()._
- Description: A brief description of the plugin’s functionality. For example:
A Telegraph plugin that allows you to publish your content easily
- Plugin name: A unique identifier for the plugin. For example:
-
Select Development Language: When prompted
Select language
, choosepython
. -
Select Plugin Type: When prompted
Select plugin type
, for this tutorial, choosetool
. -
Select Additional Features: Next, you’ll be prompted if you need to include Provider validation, persistent storage, and other additional features. For this simple Hello World plugin, we don’t need these yet, so you can press Enter to skip all options until you see the success message.
-
Confirm Creation Success: When the terminal outputs information similar to the following, it indicates that the plugin project has been successfully created:
Now, a new folder named telegraph
(or the plugin name you specified) should appear in your current directory, which is your plugin project.
3. Configuring Python Virtual Environment and Dependencies
To isolate project dependencies, we recommend using a Python virtual environment.
3.1 Creating and Activating a Virtual Environment (Command Line Method)
This is the recommended and universal method, not dependent on any specific IDE:
-
Navigate to the Project Directory:
-
Create a Virtual Environment: (Recommended to name it
venv
) -
Activate the Virtual Environment:
-
macOS / Linux:
-
Windows (cmd.exe):
-
Windows (PowerShell):
-
After successful activation, your terminal prompt will typically display
(venv)
at the beginning.
-
3.2 Installing Basic Dependencies
The requirements.txt
file generated during project initialization already includes the basic library dify_plugin
needed for plugin development. After activating the virtual environment, execute the following command to install:
3.3 (Optional) VSCode Integrated Environment Configuration
If you use VSCode as your code editor, you can leverage its integrated features to manage the Python environment:
- Open the Project Folder: Use VSCode to open the
telegraph
folder you just created. - Select Python Interpreter:
- Open the command palette (macOS:
Cmd+Shift+P
, Windows/Linux:Ctrl+Shift+P
). - Type and select
Python: Select Interpreter
. - In the pop-up list, select the Python interpreter in the virtual environment you just created (usually the path includes
.venv/bin/python
orvenv\Scripts\python.exe
). If the list doesn’t automatically display it, you can selectEnter interpreter path...
to manually find it. - (Please refer to your local corresponding screenshot, which shows the interpreter selection interface)
- Open the command palette (macOS:
- Install Dependencies (If VSCode Prompts): VSCode may detect the
requirements.txt
file and prompt you to install its dependencies. If prompted, confirm the installation.- (Please refer to your local corresponding screenshot, which shows the interface for confirming dependency installation)
Please ensure that all subsequent pip install
commands and running python -m main
operations are performed in the activated virtual environment.
4. Developing the Plugin Core Logic
Now let’s write the plugin code. This example will implement a simple tool for publishing specified content to Telegraph.
4.1 Example Dependency Library: your-telegraph
We will use a Python library called your-telegraph
to interact with the Telegraph API. (This is a hypothetical library name, please ensure that the library you actually use is valid).
your-telegraph
is a Python wrapper that simplifies Telegraph API operations, allowing you to easily publish content with just a few lines of code.
Its basic usage might be as follows:
Our goal is to implement similar functionality in a Dify plugin.
4.2 Adding and Configuring Project Dependencies
-
Install the Dependency Library: Ensure your virtual environment is activated, then execute in the terminal:
-
Update
requirements.txt
: Open therequirements.txt
file in thetelegraph
project root directory, and add a line belowdify_plugin
with the name of the library you just installed:This ensures that other developers or deployment environments can easily install all the required dependencies.
4.3 Configuring Provider Credentials
Our example requires a telegraph_access_token
. We need to define this credential in the Provider configuration so that users can input it when using the plugin. For more information about Provider configuration, please refer to General Specification Definitions.
-
Edit the Provider YAML: Open the
telegraph/provider/telegraph.yaml
file. -
Add
credentials_for_provider
: Add the following content at the end of the file (or at an appropriate location):- Field Explanations:
telegraph_access_token
: Unique identifier for the credential, accessed in code viaself.runtime.credentials["telegraph_access_token"]
.type: secret-input
: Indicates that it will be displayed as a password input field in the Dify interface.required: true
: Indicates that users must fill in this credential to use tools provided by this plugin.label
,placeholder
,help
: Provide multilingual interface text.url
: (Optional) Provides a help link for obtaining the credential.
- Field Explanations:
4.4 Implementing Tool Logic
Now let’s write the code that actually performs the publishing operation.
-
Edit the Tool Python File: Open
telegraph/tools/telegraph.py
. -
Implement the
_invoke
Method: Replace the file contents with the following code:- Key Points:
- Get credentials from
self.runtime.credentials
. - Get the tool’s input parameters from
tool_parameters
(parameter names will be defined in YAML in the next step). Using.get()
is a more robust approach. - Call the
ytelegraph
library to perform the actual operation. - Use
try...except
to catch possible errors and throw exceptions. - Use
yield self.create_link_message(url)
to return a result containing a URL to Dify.
- Get credentials from
- Key Points:
4.5 Configuring Tool Parameters
We need to tell Dify which input parameters this tool accepts.
-
Edit the Tool YAML File: Open
telegraph/tools/telegraph.yaml
. -
Define Parameters: Replace or modify the file contents to:
- Field Explanations:
identity
: Basic information about the tool,name
is a unique identifier.description
: Divided intohuman
(for users) andllm
(for Agent). Thellm
description is crucial for the Agent to correctly understand and use the tool.parameters
: Defines each input parameter.name
: Internal name, must match the key in the Python code’stool_parameters.get("...")
.type
: Data type (such asstring
,number
,boolean
, etc.).required
: Whether it must be provided.label
,human_description
,llm_description
: Similar to descriptions inidentity
, but for specific parameters.llm_description
should clearly guide the LLM on how to generate or obtain the parameter value, including format requirements (such as Markdown here).form
: Defines how the parameter is presented and filled in Dify.llm
indicates that the parameter value can be input by the user, passed through variables, or determined by the LLM in Agent mode;form
typically indicates a configuration item that needs to be fixed by the user in the interface. For tool inputs,llm
is more common.
extra.python.source
: Specifies the path to the Python file implementing this tool’s logic (relative to the project root directory).
- Field Explanations:
4.6 Implementing Provider Credential Validation (Optional but Recommended)
To ensure that the credentials provided by users are valid, we should implement validation logic.
-
Edit the Provider Python File: Open
telegraph/provider/telegraph.py
. -
Implement the
_validate_credentials
Method: Replace the file contents with:- Key Points:
- Get the credentials from the
credentials
dictionary. - Perform an API call that requires the credential (preferably a read-only operation like getting account information; if not available, creating a harmless test page is also acceptable, but be aware of potential side effects).
- If the API call succeeds, don’t throw an exception, indicating validation passed.
- If the API call fails, catch the exception and throw a
ToolProviderCredentialValidationError
, including the original error message.
- Get the credentials from the
- Key Points:
5. Local Running and Debugging
Now you can run the plugin locally and debug it in Dify.
-
Prepare the
.env
File:-
Make sure you’re still in the
telegraph
project directory. -
Copy the environment variable template file:
-
Edit the
.env
File: Open the.env
file you just created and fill in your Dify environment information:- Get Host and Key: Log in to your Dify environment, click the “Plugins” icon in the top right corner, then click the debug icon (or something that looks like a bug). In the pop-up window, copy the “API Key” and “Host Address”. (Please refer to your local corresponding screenshot, which shows the interface for obtaining the key and host address)
-
-
Start the Local Plugin Service:
-
Ensure your Python virtual environment is activated.
-
In the
telegraph
directory, run the main program: -
Observe Terminal Output: If everything is normal, you should see log information similar to the following, indicating that the plugin tool has been successfully loaded and connected to Dify:
-
-
View and Test in Dify:
- Refresh the Dify Page: Return to your Dify environment (in the browser), refresh the plugin management page (typically
https://your-dify-host.com/plugins
). - Find the Plugin: You should see a plugin named “Telegraph” (or the
label
you defined in the Provider YAML) in the list, possibly with a “Debugging” mark. - Add Credentials: Click on the plugin, and you’ll be prompted to enter the “Telegraph Access Token” you defined earlier in
provider/telegraph.yaml
. Enter a valid token and save. If your validation logic (_validate_credentials
) is implemented correctly, validation will be performed here. (Please refer to your local corresponding screenshot, which shows the plugin appearing in the list and requesting authorization) - Use in Applications: Now, you can add this tool node in Dify applications (such as Chatbot or Workflow) and try to call it! When you run the application and trigger the tool, the request will be forwarded to your local
python -m main
process for processing. You can see related log output in your local terminal for debugging.
- Refresh the Dify Page: Return to your Dify environment (in the browser), refresh the plugin management page (typically
-
Stop the Local Service: Press
Ctrl + C
in the terminal to stop the local plugin service.
This run -> test -> stop -> modify code -> run again cycle is the main workflow for plugin development.
6. Enhancing Plugin Metadata
To make the plugin more professional, discoverable, and understandable, we need to enhance some display information.
-
Icon:
- Place an icon file representing your plugin in the
telegraph/_assets
directory (e.g.,icon.png
,icon.svg
). Square, clear images are recommended.
- Place an icon file representing your plugin in the
-
Provider Information (
provider/telegraph.yaml
):- Ensure that the
label
(display name),description
(function description), andicon
(icon filename, such asicon.png
) in theidentity
section are filled in and support multiple languages. This information is primarily displayed to users who use the plugin in the Dify application orchestration interface.
- Ensure that the
-
Plugin Manifest (
manifest.yaml
):- Edit the
manifest.yaml
file in the project root directory. This is the “ID card” for the entire plugin, and its information will be displayed on the plugin management page and Plugin Marketplace in Dify. - Be sure to update or confirm the following fields:
label
: The main display name of the plugin (supports multiple languages).description
: An overall introduction to the plugin’s functionality (supports multiple languages), which should clearly summarize its core value. Note that the marketplace display may have length limitations.icon
: The main icon of the plugin (directly enter the icon filename in the_assets
directory, such asicon.png
).tags
: Add category tags to the plugin, which helps users filter in the marketplace. For optional values, please refer to Dify’s enumerations or documentation (such asmedia
,tools
,data-processing
, etc.). You can refer to the ToolLabelEnum definition.
- Edit the
-
README and Privacy Policy:
README.md
: Edit theREADME.md
file in the project root directory. It will serve as the detailed introduction page for the plugin on the Marketplace, and should include richer information such as detailed functionality, usage examples, configuration guide, frequently asked questions, etc. You can refer to the style of the AWS plugin marketplace page.PRIVACY.md
: If you plan to publish the plugin to the official Marketplace, you need to provide a privacy policy documentPRIVACY.md
, describing how the plugin handles data.
7. Packaging the Plugin
When plugin development is complete and passes local testing, you can package it into a .difypkg
file for distribution or installation. For detailed information about plugin packaging and publishing, please refer to Publishing Overview.
-
Return to Parent Directory: Ensure your terminal’s current path is at one level above the
telegraph
folder. -
Execute the Packaging Command:
(Replace
./telegraph
with the actual path to your plugin project) -
Get the Package File: After successful execution, a file named
telegraph.difypkg
(oryour_plugin_name.difypkg
) will be generated in the current directory.
This .difypkg
file is a complete plugin package. You can:
- Upload and install it manually on the Dify plugin management page.
- Share it with others to install.
- According to Dify’s specifications and processes, publish it to the official Plugin Marketplace, allowing all Dify users to discover and use your plugin. For specific publishing procedures, please refer to Publish to Dify Marketplace.
Congratulations! You have completed the entire process of developing, debugging, enhancing, and packaging your first Dify plugin. Now you can explore more complex and powerful plugin features based on this foundation.
Next Learning Steps
- Remote Debugging Plugins - Learn more advanced plugin debugging techniques
- Persistent Storage - Learn how to use data storage in plugins
- Slack Bot Plugin Development Example - View a more complex plugin development case
- Tool Plugin - Explore advanced features of tool plugins