A CRUD example of using Cloudflare Workers to create a simple API that store and retrieve data from D1 storage.
You will get a url like this: https://difytoolsworker.yourname.workers.dev
In Dify tools, you can create a new custom tool and use the https://difytoolsworker.yourname.workers.dev/doc
as the endpoint.
Click import from URL you deployed from Cloudflare Workers.
Set the Authorization header with the token you set in the wrangler.toml file. Mine is bananaiscool
.
Click Save and you are done.
You can now use the tool in Dify Workflows.
Not Found
Found
In D1 Console
npm install
cp wrangler.toml.example wrangler.toml
npm run dev
# Run this command to create a new database
npx wrangler d1 create <DATABASE_NAME>
# You will get things like this:
[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "dify-global-store"
database_id = "xxxx-xxx-xxx-xxx-xxxx"
# Paste the things above to the wrangler.toml file
:( Do not do this
npx wrangler d1 execute <DATABASE_NAME> --local --file=./db/migrations/1.sql
npx wrangler d1 execute <DATABASE_NAME> --local --command="SELECT * FROM Users"
:) Do this
For readable purpose, we use D1 Migrations to manage database migrations.
npx wrangler d1 migrations create <DATABASE_NAME> users
npx wrangler d1 migrations apply <DATABASE_NAME> --local
npx wrangler d1 migrations list <DATABASE_NAME> --local
npm run deploy