Skip to main content

Procedure

Since Dify custom endpoints require a publicly accessible address as the API Endpoint, the endpoint needs to be deployed to a public address. Here we use Cloudflare Workers to deploy the custom endpoint. Clone the Example GitHub Repository. This repository contains a simple custom endpoint that can be modified as a starting point.
Open the wrangler.toml file and modify name and compatibility_date to your application name and compatibility date. The configuration we need to pay attention to here is the TOKEN in vars. When adding a custom endpoint in Dify, we need to fill in this Token. For security reasons, we recommend using a random string as the Token. You should not write the Token directly in the source code, but pass it through environment variables. Therefore, please do not commit wrangler.toml to your code repository.
This custom endpoint will return a random Breaking Bad quote. You can modify the logic of this custom endpoint in src/index.ts. This example demonstrates how to interact with third-party APIs.
This repository simplifies all configurations except business logic. You can directly use npm commands to deploy your custom endpoint.
After successful deployment, you will get a public address that you can add in Dify as the custom endpoint. Please note not to omit the endpoint path. The specific definition of this path can be found in src/index.ts.
Add Custom Endpoint in Dify
Alternatively, you can use the npm run dev command to deploy locally for testing.
Related output:
After that, you can use tools like Postman for local interface debugging.

About Bearer Auth

Our Bearer validation logic is in the above code. We use the hono/bearer-auth package to implement Bearer validation. You can use c.env.TOKEN in src/index.ts to get the Token.

About Parameter Validation

We use zod to define parameter types here. You can use zValidator in src/index.ts to validate parameters. Use const { point, params } = c.req.valid("json"); to get the validated parameters. The point here has only two values, so we use z.union to define it. params is an optional parameter, so we use z.optional to define it. There will be an inputs parameter, which is a Record<string, any> type. This type represents an object with string keys and any values. This type can represent any object. You can use params?.inputs?.count in src/index.ts to get the count parameter.

Get Cloudflare Workers Logs


Reference:
Last modified on June 22, 2026