How to Build a REST API with CodeIgniter 3
N
Back to Blog

How to Build a REST API with CodeIgniter 3

Tutorial
Nugroho Setiawan 02 Apr 2026 7 min baca 289 kata 112 views
A complete tutorial on building a REST API with the CodeIgniter 3 framework. From setup to endpoint testing.

Introduction

REST API (Representational State Transfer Application Programming Interface) is the standard approach for communication between applications over HTTP. In this tutorial, we will learn how to build a REST API using the CodeIgniter 3 framework — one of the most popular PHP frameworks in Indonesia.

Why CodeIgniter 3?

CodeIgniter 3 is an excellent choice for building REST APIs because:

  • Lightweight and fast — minimal footprint with maximum performance
  • Easy to learn — especially for developers already familiar with PHP
  • Flexible — no rigid structure, giving you freedom in architecture
  • Comprehensive documentation — clear and well-organized official docs

Project Setup

Follow these steps to set up your CodeIgniter 3 project for REST API development:

  1. Download CodeIgniter 3 from the official website and extract it to your web server directory
  2. Configure the database in application/config/database.php
  3. Set the base URL in application/config/config.php
  4. Enable query strings if needed for API parameters

Creating an API Controller

Create a new controller that will handle API requests. The controller should output JSON responses instead of HTML views:

class Api extends CI_Controller {
    public function users_get() {
        $users = $this->db->get('users')->result();
        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(['status' => 'success', 'data' => $users]));
    }
}

HTTP Methods

A proper REST API uses different HTTP methods for different operations:

  • GET — Retrieve data
  • POST — Create new data
  • PUT — Update existing data
  • DELETE — Remove data

Testing the API

Use tools like Postman or cURL to test your API endpoints. Make sure to test all CRUD operations and edge cases like invalid input and authentication.

Conclusion

Building a REST API with CodeIgniter 3 is straightforward and efficient. The key principles are: use proper HTTP methods, always return JSON responses, implement proper error handling, and secure your endpoints with authentication.

Terakhir diperbarui 23 Jun 2026

Komentar

Komentar ditinjau sebelum tampil.

Belum ada komentar. Jadilah yang pertama!