To create a custom tool in Dify, you must provide an OpenAPI-Swagger specification (JSON) that describes your API. Dify uses this schema to understand the available endpoints, required parameters, and data structures.
Steps to create a tool:
- Ensure your external API (e.g., a FastAPI service) is publicly accessible.
- Generate an OpenAPI 3.1.0 JSON schema. You can use LLMs like GPT to convert
curl commands into the required schema. - In Dify, navigate to the Custom Tool creation section and paste your JSON schema.
- Test the interface within Dify to verify that the returned values match your API's expected output.
{
"openapi": "3.1.0",
"info": {
"title": "Generate Image API",
"description": "API to generate an image based on a given prompt.",
"version": "v1.0.0"
},
"servers": [
{
"url": "http://YOUR_API_ENDPOINT"
}
],
"paths": {
"/generate_image/": {
"post": {
"summary": "Generate an image based on a prompt",
"operationId": "generateImage",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GenerateImageRequest"
}
}
}
},
"responses": {}
}
}
},
"components": {
"schemas": {
"GenerateImageRequest": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The prompt describing the image to be generated."
}
},
"required": [
"prompt"
]
}
}
}
}