Serverless RESTful API
Built for Cloudflare Workers. uses Cloudflare KV 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'
-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/faculty- Access Faculty Members Data/member- Access Regular Members Data/event- 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", teams: [Array] # Optional in Creation }
CRUD Operation
Accepts
POST /<API_ROUTE> {json in body}- Create Single DataGET /<API_ROUTE>- Read All DataGET /<API_ROUTE>/<ID>- Read Single DataPUT /<API_ROUTE>/<ID>- Update Single DataDELETE /<API_ROUTE>- Delete All DataDELETE /<API_ROUTE>/<ID>- Delete Single Data
Returns
Create
- Returns
http - 200ID and Collection of new data in JSON on Success - Returns
http - 409on Data Conflict - Returns
http - 422on Unprocessable Entity - Returns
http - 500on Creation error
- Returns
Read
- Returns
http - 200requested data in JSON on Success - Returns
http - 404on Not Found
- Returns
Update
- Returns
http - 200Request Permitted! on Success - Returns
http - 404on Not Found - Returns
http - 422on Unprocessable Entity - Returns
http - 500on Update error
- Returns
Delete
- Returns
http - 200Request Permitted! on 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-KV.git
Rename
wrangler.example.tomlfile towrangler.tomlRename
example.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 KV Namespace in Cloudflare
wrangler kv:namespace create YOUR_NAMESPACE
NOTE: Replace your own desired Database Name with
YOUR_NAMESPACEOn Hitting Enter, an ID of the created namespace will be returned.
Put Binding in
wrangler.tomlfileReplace
with the ID you got on namespace Creation NOTE: ❌ Don't Edit the Binding Name
binding = "DATABASE"
3. Deploy Project
To Deploy/Test in Local
wrangler dev
To Deploy in Cloudflare
wrangler deploy
You can change the project name by editing the
namefield fromwrangler.tomlfile before deploying your project
Hosted Demo 🐬
Live @Cloudflare
Test
curl https://restapi-kv-demo.xrontrix.workers.dev
Returns
Server is up and running!