Built for Cloudflare Workers. uses Cloudflare D1 Sqlite as Database
Blazing Fast Serverless 🔥
- TOKEN based Authentication System
- Every type Error Handling
- Real time http response code
Need to pass Authorization header along with AUTH_TOKEN. Otherwise it will return Unauthorized (http - 401)
curl -x POST https://backend.workers.dev/faculty \
-H 'Authorization: AUTH_TOKEN' \ # required for authentication
-d '{name: "Xron Trix"}'
200– OK204– No Content400– Bad Request401– Unauthorized404– Not Found405– Method not Allowed409– Data conflicts422– Unprocessable Entity500– Server error
/– Check if Server is Online/faculties– Access Faculty Members Data/members– Access Regular Members Data/events– Access Event Data
-
Faculty
{ name: "String", role: "String", image: "String", mobile: "String" } -
Member
{ name: "String", role: "String", image: "String", mobile: "String", roll: "String" } -
Event
{ title: "String", page: "String", image: "String" }
POST /<API_ROUTE> {json in body}– Create Single DataGET /<API_ROUTE>– Read All DataGET /<API_ROUTE>/<ID>– Read Single DataGET /<API_ROUTE>?<KEY>=<VALUE>– Filter Data by KeyPUT /<API_ROUTE>/<ID>– Update Single DataDELETE /<API_ROUTE>– Delete All DataDELETE /<API_ROUTE>/<ID>– Delete Single Data
{
"id": # ID of the object (if any or null),
"collection": # Table Name of the Data,
"results": [
{
# Query results object array (if any or null)
}
],
"time": # Time taken on Operation in milliseconds,
"success": # Boolean value
}
-
Create
- Returns
http - 200on Success - Returns
http - 409on Data Conflict - Returns
http - 422on Unprocessable Entity - Returns
http - 500on Creation error
- Returns
-
Read
- Returns
http - 200on Success - Returns
http - 404on Not Found
- Returns
-
Update
- Returns
http - 200on Success - Returns
http - 404on Not Found - Returns
http - 422on Unprocessable Entity - Returns
http - 500on Update error
- Returns
-
Delete
- Returns
http - 200on Success - Returns
http - 404on Not Found - Returns
http - 500on Update error
- Returns
-
Create a Cloudflare Account if haven’t already 🙂
-
Create a subdomain for your workers pages.
Your Projects will be visible as
https://project.SUB_DOMAIN.workers.dev
-
Install node.js if haven’t already 🙂
-
Install Wrangler as
npm install wrangler --save-dev
-
Login with Cloudflare Account
wrangler login
-
Clone This Repository
git clone https://github.com/XronTrix10/Cloudflare-RESTful-D1.git
-
Rename
wrangler.sample.tomlfile towrangler.toml -
Rename
sample.dev.varsfile to.dev.vars -
Put a secret Auth Token in
.dev.varsfile. This Token will be used to Authenticate You with Your API -
Put the auth token in Wrangler as well
wrangler secret put AUTH_TOKEN
Enter The Same Token you Placed inside
.dev.varsfile -
Install dependencies
npm install
-
Create a D1 Database in Cloudflare
wrangler d1 create DATABASE_NAME
📝 NOTE: Replace your own desired Database Name with
DATABASE_NAMEOn Hitting Enter, an ID of the created namespace will be returned.
-
Put Binding in
wrangler.tomlfile- Replace
<DATABASE_NAME>with Your Chosen Database Name and<unique-ID-for-your-database>with the ID you got on Database Creation
📝 NOTE: ❌ Don’t Edit the Binding Name
binding = "DB" - Replace
-
To Deploy/Test in Local
wrangler dev
-
To Deploy in Cloudflare
wrangler deploy
📝 NOTE: You can change the project name by editing the
namefield fromwrangler.tomlfile before deploying your project
Live @Cloudflare
curl https://restapi-d1.vilgax.workers.dev
Server is up and running!

Leave a Reply