
Serverless RESTful API
Built for Cloudflare Workers. uses Cloudflare D1 Sqlite as Database
Blazing Fast Serverless 🔥
Features 🚀
- TOKEN based Authentication System
- Every type Error Handling
- Real time http response code
Authentication
Need to pass Authorization header along with AUTH_TOKEN. Otherwise it will return Unauthorized (http - 401)
Example
curl -x POST https://backend.workers.dev/faculty \
-H 'Authorization: AUTH_TOKEN' \ # required for authentication
-d '{name: "Xron Trix"}'
Available HTTP Responses
200- OK204- No Content400- Bad Request401- Unauthorized404- Not Found405- Method not Allowed409- Data conflicts422- Unprocessable Entity500- Server error
Allowed HTTP Methods
GETPOSTPUTDELETE
Api Routes
/- Check if Server is Online/faculties- Access Faculty Members Data/members- Access Regular Members Data/events- Access Event Data
JSON Data Formats
Unique ID Will be Auto Added on Creation on Each Data
Each Field is Required on POST request
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" }
CRUD Operation
Accepts
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
Returns
Returns an Object of Following Properties on Success
{
"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
}
Also Returns HTTP status
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
Deploy Step by Step 🦀
1. Set up Cloudflare
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
2. Set up Project
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.tomlRename
sample.dev.varsfile to.dev.varsPut a secret Auth Token in
.dev.varsfile. This Token will be used to Authenticate You with Your APIPut the auth token in Wrangler as well
wrangler secret put AUTH_TOKEN
Enter The Same Token you Placed inside
.dev.varsfileInstall 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.tomlfileReplace
<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"
3. Deploy Project
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
Hosted Demo 🐬
Live @Cloudflare
Test
curl https://restapi-d1.vilgax.workers.dev
Returns
Server is up and running!