Skip to content

Repository files navigation

InstaApp Backend API

A modern Instagram-like backend application built with Laravel 12, providing RESTful APIs for social media functionality including posts, comments, and likes.

Table of Contents

Tech Stack

  • Framework: Laravel 12.x
  • PHP: ^8.2
  • Database: PostgreSQL
  • Authentication: Laravel Sanctum
  • File Storage: Cloudflare R2 (S3-compatible via Flysystem)
  • ID Generation: ULID (26-character sortable IDs)

Features

  • 🔐 Authentication (Register, Login, Logout)
  • 👤 User Profile Management
  • 📸 Post Creation & Management (with image upload)
  • 💬 Comments on Posts
  • ❤️ Like/Unlike Posts
  • 🔒 Secure API with Sanctum Token Authentication
  • 📦 Optimized Database Queries with Relationships
  • 🎨 RESTful API Design

Database Structure

The application uses a relational database with the following tables:

Users Table

Column Type Constraints
id char(26) PRIMARY KEY, ULID
name varchar(255) NOT NULL
email varchar(100) UNIQUE, NOT NULL
password varchar(255) NOT NULL
created_at timestamp NOT NULL
updated_at timestamp NOT NULL

Posts Table

Column Type Constraints
id char(26) PRIMARY KEY, ULID
caption text NULLABLE
image_path text NOT NULL
user_id char(26) FOREIGN KEY → users.id
created_at timestamp NOT NULL
updated_at timestamp NOT NULL

Comments Table

Column Type Constraints
id char(26) PRIMARY KEY, ULID
text varchar(500) NOT NULL
post_id char(26) FOREIGN KEY → posts.id
user_id char(26) FOREIGN KEY → users.id
created_at timestamp NOT NULL
updated_at timestamp NOT NULL

Likes Table

Column Type Constraints
id char(26) PRIMARY KEY, ULID
user_id char(26) FOREIGN KEY → users.id
post_id char(26) FOREIGN KEY → posts.id
created_at timestamp NOT NULL
updated_at timestamp NOT NULL

Database Relationships

users (1) ──────< (many) posts
users (1) ──────< (many) comments
users (1) ──────< (many) likes
posts (1) ──────< (many) comments
posts (1) ──────< (many) likes

All foreign key relationships have ON DELETE CASCADE to maintain referential integrity.

API Endpoints

All API endpoints are prefixed with /api/v1.

Authentication Endpoints

Method Endpoint Description Auth Required
POST /auth/register Register new user
POST /auth/login Login user
GET /auth/me Get authenticated user profile
POST /auth/logout Logout user

Post Endpoints

Method Endpoint Description Auth Required
GET /posts Get all posts (paginated)
GET /posts/me Get authenticated user's posts
GET /posts/{id} Get single post
POST /posts Create new post
PUT /posts/{id} Update post
DELETE /posts/{id} Delete post

Comment Endpoints

Method Endpoint Description Auth Required
GET /posts/{id}/comments Get all comments for a post
POST /posts/{id}/comments Create comment on a post
PUT /comments/{id} Update comment
DELETE /comments/{id} Delete comment

Like Endpoints

Method Endpoint Description Auth Required
POST /posts/{id}/like Toggle like on a post

Installation

Prerequisites

  • PHP 8.2 or higher
  • Composer
  • PostgreSQL
  • Node.js & NPM (for frontend assets)

Setup Steps

  1. Clone the repository

    git clone <repository-url>
    cd InstaApp-be
  2. Install dependencies

    composer install
    npm install
  3. Environment configuration

    cp .env.example .env
  4. Generate application key

    php artisan key:generate
  5. Configure database

    Update your .env file with database credentials:

    DB_CONNECTION=pgsql
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=instaapp_be
    DB_USERNAME=your_username
    DB_PASSWORD=your_password
  6. Configure Cloudflare R2 (for image uploads)

    Update your .env file with Cloudflare R2 credentials:

    CLOUDFLARE_ACCESS_KEY_ID=your_r2_access_key_id
    CLOUDFLARE_SECRET_ACCESS_KEY=your_r2_secret_access_key
    CLOUDFLARE_REGION=auto
    CLOUDFLARE_BUCKET=your_r2_bucket_name
    CLOUDFLARE_ENDPOINT=https://<account_id>.r2.cloudflarestorage.com
    CLOUDFLARE_URL=your_public_r2_url

    Note: Cloudflare R2 is S3-compatible and uses Flysystem for storage operations.

  7. Run migrations

    php artisan migrate
  8. Build frontend assets

    npm run build

Configuration

Quick Setup (Automated)

Run the setup script to install all dependencies and configure the application:

composer setup

This command will:

  • Install PHP dependencies
  • Create .env file from example
  • Generate application key
  • Run database migrations
  • Install NPM packages
  • Build frontend assets

Running the Application

Development Mode

Run the development server with hot-reload and queue worker:

composer dev

This command runs three processes concurrently:

  • Laravel development server (port 8000)
  • Queue worker
  • Vite development server

Manual Start

Alternatively, start services individually:

# Start Laravel server
php artisan serve

# Start queue worker (in another terminal)
php artisan queue:listen

# Start Vite dev server (in another terminal)
npm run dev

The API will be available at http://localhost:8000.

License

This project is open-sourced software licensed under the MIT license.

About

Instagram like clone app backend, built using Laravel

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages