Skip to content

Serverless RESTful API Built for Cloudflare Workers. uses Cloudflare D1 Sqlite as Database

Notifications You must be signed in to change notification settings

XronTrix10/Cloudflare-RESTful-D1

Repository files navigation

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 - OK
  • 204 - No Content
  • 400 - Bad Request
  • 401 - Unauthorized
  • 404 - Not Found
  • 405 - Method not Allowed
  • 409 - Data conflicts
  • 422 - Unprocessable Entity
  • 500 - Server error

Allowed HTTP Methods

  • GET
  • POST
  • PUT
  • DELETE

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 Data
  • GET /<API_ROUTE> - Read All Data
  • GET /<API_ROUTE>/<ID> - Read Single Data
  • GET /<API_ROUTE>?<KEY>=<VALUE> - Filter Data by Key
  • PUT /<API_ROUTE>/<ID> - Update Single Data
  • DELETE /<API_ROUTE> - Delete All Data
  • DELETE /<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 - 200 on Success
    • Returns http - 409 on Data Conflict
    • Returns http - 422 on Unprocessable Entity
    • Returns http - 500 on Creation error
  • Read

    • Returns http - 200 on Success
    • Returns http - 404 on Not Found
  • Update

    • Returns http - 200 on Success
    • Returns http - 404 on Not Found
    • Returns http - 422 on Unprocessable Entity
    • Returns http - 500 on Update error
  • Delete

    • Returns http - 200 on Success
    • Returns http - 404 on Not Found
    • Returns http - 500 on Update error

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.toml file to wrangler.toml

  • Rename sample.dev.vars file to .dev.vars

  • Put a secret Auth Token in .dev.vars file. 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.vars file

  • Install dependencies

    npm install
  • Create a D1 Database in Cloudflare

    wrangler d1 create DATABASE_NAME

    📝 NOTE: Replace your own desired Database Name with DATABASE_NAME

    On Hitting Enter, an ID of the created namespace will be returned.

  • Put Binding in wrangler.toml file

    • 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"

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 name field from wrangler.toml file before deploying your project


Hosted Demo 🐬

Live @Cloudflare

Test

curl https://restapi-d1.vilgax.workers.dev

Returns

Server is up and running!