Do you know what APIs are? Well, API stands for Application Programming Interface, which allows the communication between different applications. In simpler terms, it can be described as a contract between different software components. In today’s digital world, APIs have become an essential component for various businesses in enabling them to connect their platforms and services to other companies, services and applications, providing new and innovative ways of offering their services.
One of the most popular types of API is the REST API, or representational state transfer API, which is a simple way for two applications to talk to each other over the internet. In this article, we will explore how to create a REST API using Python, Excel and CodeIgniter, and discuss the benefits of doing so.
Creating a REST API using Python
Python is an easy-to-learn programming language often used in web development, scientific computing, and artificial intelligence. It has a syntax that allows developers to write codes with fewer lines and has a massive library with open source packages. Some of the popular Python frameworks used for creating REST APIs are Flask, Django and FastAPI. In this article, we will use Flask to create a simple REST API.
To get started, you must have Python installed on your computer. You can download the latest version of Python from here. Once you have installed Python, you can install Flask using pip, a package manager for python.
To install Flask, open your command prompt or terminal and type:
pip install Flask
Once Flask is installed, create a new file called app.py
and paste the following code.
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/api', methods=['GET'])
def get_api():
return jsonify('data': 'This is a GET request!')
@app.route('/api', methods=['POST'])
def post_api():
data = request.get_json()
return jsonify('data': data['message'])
The above code creates a simple REST API that returns “Hello, World!” when accessed via the root route. It also creates two routes, /api
with GET and POST methods, where GET returns a JSON response and POST returns a JSON response with message
To run the above code, open your command prompt or terminal and navigate to the directory where the app.py
file is saved, then type:
python app.py
Your REST API is now running locally on your computer, and you can test it by sending a GET and POST request through the URL provided by Flask.
Creating a REST API using Excel
Excel is one of the most widely used programs for data management and data analysis. Excel can also be used to create a REST API, and it’s simpler than you think. In this section, we will explore how to create a REST API using Excel.
The first thing you need to do is to create an Excel file with your data. For this example, let’s say we have a file named testdata.xlsx
with the following data:
Id | First Name | Last Name | |
---|---|---|---|
1 | John | Doe | [email protected] |
2 | Jane | Doe | [email protected] |
3 | Mark | Smith | [email protected] |
Next, you need to install two Excel add-ins: Power Query and Power Pivot. These two add-ins come pre-installed in Excel 2016 and later versions. To activate them, go to File
> Options
> Add-Ins
> Excel Add-ins
> Manage
> Com Add-ins
> Go
.
Select both Microsoft Power Pivot for Excel
and Microsoft Power Query for Excel
and click OK
.
After activating the add-ins, you can now create a table from the data in the Excel file and then publish the table as a REST API. To do this, follow these steps:
- Select the data in the Excel file.
- Go to the
Data
tab and click onFrom Table/Range
. This will open the Power Query Editor. - In the Power Query Editor, click on
Close & Load To…
. - Select
PivotTable Report
and clickOK
. - This will create a new sheet with the PivotTable. Rename the sheet to
API Data
. - Go to the
Power Pivot
tab and click onAdd to Data Model
. Save your Excel file. - After saving the file, click on the
Design
tab and selectImport from Excel
. - Select the Excel file and click
OK
. - This will import the data to the Power BI service.
- Select the table you want to publish as a REST API, and click on the three dots (…) at the top-right corner and select
Publish to Web
.
You can now access your published table as a REST API using the URL provided by Power BI.
Creating a REST API using CodeIgniter
CodeIgniter is a powerful PHP framework used for web development. It is a lightweight yet powerful framework designed to make coding easier, faster and more efficient. In this section, we will use CodeIgniter 4 to create a REST API.
The first thing you need to do is to download and install CodeIgniter on your computer. You can download CodeIgniter from here. Once you have downloaded and installed CodeIgniter, create a new project by running the following command in your terminal:
composer create-project codeigniter4/appstarter myproject
This will create a new CodeIgniter project named myproject
.
Next, we need to install the CodeIgniter RESTful extension, which can be done using composer. Open your terminal and navigate to the directory where your CodeIgniter project is, then run the following command:
composer require codeigniter4/restful
This will install the CodeIgniter RESTful extension. Now, create a new controller named Api.php
in your project’s Controllers
directory and add the following code:
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
class Api extends ResourceController
protected $modelName = 'App\Models\APIModel';
protected $format = 'json';
public function index()
return $this->respond($this->model->findAll());
public function create()
$data = [
'title' => $this->request->getVar('title'),
'content' => $this->request->getVar('content')
];
$this->model->insert($data);
return $this->respondCreated($data);
public function update($id = null)
$data = [
'title' => $this->request->getVar('title'),
'content' => $this->request->getVar('content')
];
$this->model->update($id, $data);
return $this->respond($data);
public function delete($id = null)
$this->model->delete($id);
return $this->respondDeleted();
The above code creates a REST API with four methods: index
, create
, update
, and delete
. It also defines the model for the API.
To test the API, we need to create a model for our API. Create a new file named APIModel.php
in your project’s Models
directory and add the following code:
namespace App\Models;
use CodeIgniter\Model;
class APIModel extends Model
protected $table = 'news';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = ['title', 'content'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
The above code defines the model for our API. It sets the table name, primary key, and allowed fields.
To test the API, run the CodeIgniter development server by typing the following command:
php spark serve
Your REST API is now running locally on your computer, and you can test it by running the following command:
curl http://localhost:8080/api/index/
You should see a JSON response of the news table data.
Benefits of creating a REST API
REST APIs are a powerful tool for connecting different applications and systems. Here are some of the benefits of creating a REST API:
- Easy integration: REST APIs are designed to be lightweight and easy to integrate with other systems.
- Scalability: REST APIs are highly scalable, and they can easily accommodate large numbers of requests.
- Flexible data formats: REST APIs can output data in different formats, including XML, JSON, and CSV, making it easy for different applications to consume the data.
- Improved efficiency: REST APIs can improve efficiency by allowing different systems to communicate with each other quickly and easily.
- Improved security: REST APIs provide improved security by providing a standard way of transmitting data between different systems.
FAQs:
What is the difference between REST API and SOAP API?
SOAP (Simple Object Access Protocol) is a protocol based on XML, and it uses Web Services Description Language (WSDL) to describe the operations offered by a web service. REST (Representational State Transfer) is an architectural style that uses HTTP methods such as GET, POST, PUT, and DELETE to perform operations on resources. REST is more flexible than SOAP and is becoming more popular due to its simplicity and flexibility. SOAP, on the other hand, is more secure and reliable but has a steeper learning curve.
Can I use REST API with my mobile application?
Yes, you can use REST API with your mobile application. REST APIs are highly scalable and support different data formats, including JSON, which is widely used in mobile development. Many mobile applications use REST APIs to communicate with servers and retrieve data.