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.
Before starting Dify plugin development, ensure you have the following tools ready in your environment:
dify-plugin-daemon
or “Plugin Development SDK.”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.
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:
telegraph
your-name
A Telegraph plugin that allows you to publish your content easily
Select Development Language: When prompted Select language
, choose python
.
Select Plugin Type: When prompted Select plugin type
, for this tutorial, choose tool
.
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.
To isolate project dependencies, we recommend using a Python virtual environment.
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.
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:
If you use VSCode as your code editor, you can leverage its integrated features to manage the Python environment:
telegraph
folder you just created.Cmd+Shift+P
, Windows/Linux: Ctrl+Shift+P
).Python: Select Interpreter
..venv/bin/python
or venv\Scripts\python.exe
). If the list doesn’t automatically display it, you can select Enter interpreter path...
to manually find it.requirements.txt
file and prompt you to install its dependencies. If prompted, confirm the installation.
Please ensure that all subsequent pip install
commands and running python -m main
operations are performed in the activated virtual environment.
Now let’s write the plugin code. This example will implement a simple tool for publishing specified content to Telegraph.
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.
Install the Dependency Library: Ensure your virtual environment is activated, then execute in the terminal:
Update requirements.txt
: Open the requirements.txt
file in the telegraph
project root directory, and add a line below dify_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.
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):
telegraph_access_token
: Unique identifier for the credential, accessed in code via self.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.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:
self.runtime.credentials
.tool_parameters
(parameter names will be defined in YAML in the next step). Using .get()
is a more robust approach.ytelegraph
library to perform the actual operation.try...except
to catch possible errors and throw exceptions.yield self.create_link_message(url)
to return a result containing a URL to Dify.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:
identity
: Basic information about the tool, name
is a unique identifier.description
: Divided into human
(for users) and llm
(for Agent). The llm
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’s tool_parameters.get("...")
.type
: Data type (such as string
, number
, boolean
, etc.).required
: Whether it must be provided.label
, human_description
, llm_description
: Similar to descriptions in identity
, 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).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:
credentials
dictionary.ToolProviderCredentialValidationError
, including the original error message.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:
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:
https://your-dify-host.com/plugins
).label
you defined in the Provider YAML) in the list, possibly with a “Debugging” mark.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)python -m main
process for processing. You can see related log output in your local terminal for debugging.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.
To make the plugin more professional, discoverable, and understandable, we need to enhance some display information.
Icon:
telegraph/_assets
directory (e.g., icon.png
, icon.svg
). Square, clear images are recommended.Provider Information (provider/telegraph.yaml
):
label
(display name), description
(function description), and icon
(icon filename, such as icon.png
) in the identity
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.Plugin Manifest (manifest.yaml
):
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.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 as icon.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 as media
, tools
, data-processing
, etc.). You can refer to the ToolLabelEnum definition.README and Privacy Policy:
README.md
: Edit the README.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 document PRIVACY.md
, describing how the plugin handles data.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
(or your_plugin_name.difypkg
) will be generated in the current directory.
This .difypkg
file is a complete plugin package. You can:
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.
Edit this page | Report an issue