PHP restful API with sim-rest ( only 10 KB )

What is sim-rest?
sim-rest is a super lightweight, file-based API and it will help you build an API under a few minutes.
Why another library?
As a frontend developer, I was looking for a simple API or Headless API to connect with my client side JS frameworks such as React, Vue & Angular but most of the them are very big, with many dependencies and lots of configurations. That’s why I tried to build a very simple one and it became sim-rest. Hope it helps you.
These are the amazing features of sim-rest.
- Only 10 KB (gzip)
- PHP 7+
- Router (GET, POST, DELETE, PUT)
- Pass parameters via router
- JSON file database
- JWT like token-based authentication
- Auth protected routes
- Eloquent like DB selector
- Less configuration
- Zero dependencies
Ok let’s get started
Firstly download or clone sim-rest here
After downloading, cd into sim-rest project folder
Run this command in the terminal and serve sim-rest on port 4000
php -S localhost:4000
Note: I can run this command because I have php installed in my machine. If it doesn’t work in your machine, you can also serve sim-rest via xampp or mamp server
let’s openlocalhost:4000
in the browser.

If you see as this screenshoot, sim-rest is ready on your machine.
Let’s open index.php
and add this code.
That’s it, you’ve just declared a very simple GET request on /test
route. Openhttps://localhost:4000/test
in the browser and see the magic.
Let’s make a todo collection API
To build an API, we have to follow these 3 steps.
- 1. Create a collection under collections folder
- 2. Create a user inside config.php
- 3. Create routes inside index.php

Step 1
let’s create a new todo collection,
so we have to create todos.php file under collections folder and add this below codes.
Actually this is a very simple JSON object. But for security reason, we have to add a few lines of php codes at the top of the file to protect from direct access.
Step 2
Let’s edit config.php
Initially, you’ll see two users in admin array, here, we can edit or create user (who can access the API).
Just for tutorial purpose, I’ll keep this config.php file as origin.
Step 3
Let’s create routes inside index.php

That’s it! we have a todo API. Let’s give it a try from postman.
Get the whole todo collection
As GET request isn’t protect, we don’t need to provide token. We can simply fetch/todos
route to get the whole collection.
It will return an empty array because recently when we create “todos.php” collection file, we didn’t add any data.
Create a todo via API
As we’ve declared a post request route under Protected routes as below. We have to authenticate first. Otherwise, the API won’t allow us to create a new todo.
Authentication
Let’s send a POST request to /auth route and get the Token. Here, the request data must be JSON type
Content-Type: application/json
Request credential example
{
username:"admin1",
password:"admin1"
}
If we pass the authentication, the response data will return back as below screenshot.

Now we have a token, it’s time to create a new Todo.
Grab the Token and let’s append in the header as Bearer Token

In this example, I only add two properties “completed” and “task”. We can add the properties as many as we want.
Let’s send post request and create a new todo. If it works fine, a requested todo data will return.
After creating, we can fetch “/todos” route with GET request again, there will be a new todo in a todo collection as below screenshot.

[Updated]
There are more features for filtering data like limiting and sorting.
Limit
Example of limiting 10 items
Sort
Example of order by ASC (DESC is default)
Only
Example of getting only specific keys
https://localhost:4000/todos?only=title,author
Thanks for reading this tutorial.
I hope sim-rest would help your frontend work a bit easier.
This tutorial may not cover all CRUD operations, including PUT,DELETE request, you can still access those information in the documentation.
For more about sim-rest and its’ features, you can read the documentation here