B4J Tutorial [Web][BANano] Plug n Play PHP CRUD REST API for SQLite with Authentication

Hi Fam

Previously, my post below dealt with adding a CRUD REST API in a matter of minutes to any back-end.


We also looked at how we can add authentication to this REST API on this post.


Today we look at how we can use a SQLite file for the backend with authentication also. Thanks to this Github project.

I have updated the api.php file to point to the SQLite file on the assets folder and also added the needed authentication key.

Let's do this.
 

Mashiane

Expert
Licensed User
Longtime User
Assets

1724630715699.png


1. We have the SQLIte database bibleshow.db
2. We have the api.php file. The config section has been updated to be

B4X:
$config = new Config([
        'driver' => 'sqlite',
        'address' => '../assets/bibleshow.db',
        'controllers' => 'records,openapi,status,columns',
        'debug' => true,
        'tables' => 'all',
        'middlewares' => 'sslRedirect,apiKeyAuth,sanitation,json',
        'apiKeyAuth.keys' => 'cC7vzkeSMWeWvyR8B6PgwlGv0PLkSodZ16Gpjjg8xOuwzmp3KbKvcRcR0GU1okIpsIcuq3ORpcnGvpxnyqStyrJnq5fz10y1gCqzOaisGXUfKQQ5orKLkWWu4PedieyF',
        'apiKeyAuth.header' => 'X-API-Key',
    ]);

We use the sqlite driver, give the address to point to the assets file we want. Ensure that the PDO Sqlite driver is installed on your PHP WebServer.
We have also added a couple of controllers and middlewares to make everything work.

3. We have the love.txt file, this contains the auth key that we will use via fetch api. We read this and use it for the header key 'X-API-Key'

4. Both the api.php and sqlite file (bibleshow.db) will be on the assets folder when we deploy our app.
 

Mashiane

Expert
Licensed User
Longtime User
Deployment

Our webapp is deployed on Laragon

1724631405266.png


To access the CRUD REST API, we can use POSTMAN.

OpenAPI

Let's use the OpenAPI specs to get the schema of the API

B4X:
https://localhost/sdsqliteapi/assets/api.php/openapi

1724631555479.png


Get Records from a Table

Let's read records from one of the tables, we will read the Analysis

B4X:
https://localhost/sdsqliteapi/assets/api.php/records/Analysis

1724631728071.png
 
Top