Skip to content

rajat-valecha200/blogAPI-pythonFastAPI

Repository files navigation

📝 Blog API – FastAPI + PostgreSQL + JWT Auth

A simple blog backend with user registration/login, JWT authentication, and CRUD operations for blog posts, including file uploads.

🔧 Tech Stack

  • Python 3.11+
  • FastAPI
  • SQLAlchemy
  • PostgreSQL
  • JWT (using python-jose)
  • Pydantic
  • Uvicorn

📁 Project Structure


blogAPI/
│
├── app/
│   ├── **init**.py
│   ├── main.py
│   ├── auth.py
│   ├── database.py
│   ├── models.py
│   ├── schemas.py
│   └── routes/
│       ├── users.py
│       └── posts.py
│
├── .env
├── requirements.txt
└── uploads/

⚙️ Setup Instructions

1. Clone Repo and Create Virtual Environment

git clone https://github.com/rajat-valecha200/bogAPI-pythonFastAPI.git
cd blogAPI
python -m venv venv
venv\Scripts\activate  # on Windows
# OR
source venv/bin/activate  # on Mac/Linux

2. Install Dependencies

pip install -r requirements.txt

3. Configure .env

Create a .env file in root:

DATABASE_URL=postgresql://postgres:yourpassword@localhost/blogdb
SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

Make sure PostgreSQL is running and the blogdb database is created.

4. Run the App

uvicorn app.main:app --reload

5. API Docs

Visit: http://localhost:8000/


🔐 Authentication

Use /users/register to create a user, then /users/login to get a JWT token.

Use the JWT token in the Authorization Header:

Authorization: Bearer <token>

📬 Sample API Endpoints

Method Endpoint Description
POST /users/register Register user
POST /users/login Get JWT token
POST /posts/ Create a post
GET /posts/ List all posts
GET /posts/{id} Get post by ID
PUT /posts/{id} Update post (auth only)
DELETE /posts/{id} Delete post (auth only)
POST /posts/upload/ Upload post image

📦 Sample Data (JSON)

👤 Users

{
  "email": "testuser@example.com",
  "password": "password123"
}

📝 Posts

{
  "title": "First Post",
  "content": "This is my first blog post!",
  "image": "example.png"
}

About

Basic Blog API to Login/Register User and perform CRUD operations related to Blog Posts

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages