Getting Started
Since the Dify API Extension requires a publicly accessible internet address as an API Endpoint, we need to deploy our API extension to a public internet address. Here, we use Cloudflare Workers for deploying our API extension. Clone the Example GitHub Repository, which contains a simple API extension. We can modify this as a base.wrangler.toml file, and modify name and compatibility_date to your application’s name and compatibility date.
An important configuration here is the TOKEN in vars, which you will need to provide when adding the API extension in Dify. For security reasons, it’s recommended to use a random string as the Token. You should not write the Token directly in the source code but pass it via environment variables. Thus, do not commit your wrangler.toml to your code repository.
src/index.ts. This example shows how to interact with a third-party API.
npm commands to deploy your API extension.
endpoint path.


npm run dev command to deploy locally for testing.
Other Logic TL;DR
About Bearer Auth
hono/bearer-auth package for Bearer authentication. You can use c.env.TOKEN in src/index.ts to get the Token.
About Parameter Validation
zod to define the types of parameters. You can use zValidator in src/index.ts for parameter validation. Get validated parameters through const { point, params } = c.req.valid("json");. Our point has only two values, so we use z.union for definition. params is an optional parameter, defined with z.optional. It includes a inputs parameter, a Record<string, any> type representing an object with string keys and any values. This type can represent any object. You can get the count parameter in src/index.ts using params?.inputs?.count.