Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT Security Nachrichten7 AI Security Best Practices For Deploying Generative AI In Cyber Teams(23.09.2026 um 11:31 Uhr)
IT Security NachrichtenEssential AI Security Trends Shaping Cyber Defense Strategies(23.09.2026 um 11:35 Uhr)
IT Security NachrichtenHow AI-Powered Threat Detection Catches What Traditional Tools Miss(23.09.2026 um 11:39 Uhr)
IT Security NachrichtenAI Security Guidelines And Frameworks Enterprises Need To Be Aware Of(23.09.2026 um 11:46 Uhr)
IT Security NachrichtenWhat Are the Main Security Risks Associated With Generative AI?(23.09.2026 um 11:51 Uhr)
IT Security NachrichtenHow Is GenAI Transforming Cybersecurity Strategies?(23.09.2026 um 11:53 Uhr)
IT Security NachrichtenCan AI Be Used To Effectively Prevent Cyberattacks?(23.09.2026 um 11:58 Uhr)
IT Security NachrichtenAre There Any Government Policies On Using AI For Cybersecurity?(23.09.2026 um 12:00 Uhr)
IT Security Nachrichten7 AI Security Best Practices For Deploying Generative AI In Cyber Teams(23.09.2026 um 11:31 Uhr)
IT Security NachrichtenEssential AI Security Trends Shaping Cyber Defense Strategies(23.09.2026 um 11:35 Uhr)
IT Security NachrichtenHow AI-Powered Threat Detection Catches What Traditional Tools Miss(23.09.2026 um 11:39 Uhr)
IT Security NachrichtenAI Security Guidelines And Frameworks Enterprises Need To Be Aware Of(23.09.2026 um 11:46 Uhr)
IT Security NachrichtenWhat Are the Main Security Risks Associated With Generative AI?(23.09.2026 um 11:51 Uhr)
IT Security NachrichtenHow Is GenAI Transforming Cybersecurity Strategies?(23.09.2026 um 11:53 Uhr)
IT Security NachrichtenCan AI Be Used To Effectively Prevent Cyberattacks?(23.09.2026 um 11:58 Uhr)
IT Security NachrichtenAre There Any Government Policies On Using AI For Cybersecurity?(23.09.2026 um 12:00 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

🚀 Building a CRUD Application with FastAPI – A Complete Guide

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It’s designed to be easy to use while delivering the best developer experience. In this blog, we'll walk t…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It’s designed to be easy to use while delivering the best developer experience.



In this blog, we'll walk through building a full CRUD (Create, Read, Update, Delete) application using FastAPI with a SQLite database via SQLAlchemy.









🧠 What You'll Learn




  • Setting up a FastAPI project

  • Connecting FastAPI to a SQLite database using SQLAlchemy

  • Creating database models

  • Writing API routes for CRUD operations

  • Using Pydantic models for validation

  • Testing the API using Swagger UI









📁 Project Structure






fastapi-crud/

├── app/
│ ├── main.py
│ ├── database.py
│ ├── models.py
│ ├── schemas.py
│ └── crud.py

├── requirements.txt
└── README.md












⚙️ Step 1: Setup and Installation



Create a folder and install FastAPI and dependencies:




mkdir fastapi-crud
cd fastapi-crud
python -m venv env
source env
/bin/activate
pip install fastapi uvicorn sqlalchemy pydantic






Create a requirements.txt:




fastapi
uvicorn
sqlalchemy
pydantic












🛠️ Step 2: Database Configuration – database.py






# app/database.py
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"

engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()












🧱 Step 3: Models – models.py






# app/models.py
from sqlalchemy import Column, Integer, String
from .database import Base

class Item(Base):
__tablename__ = "items"

id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String, index=True)












📦 Step 4: Schemas – schemas.py






# app/schemas.py
from pydantic import BaseModel

class ItemBase(BaseModel):
title: str
description: str

class ItemCreate(ItemBase):
pass

class Item(ItemBase):
id: int

class Config:
orm_mode = True












⚙️ Step 5: CRUD Logic – crud.py






# app/crud.py
from sqlalchemy.orm import Session
from . import models, schemas

def get_item(db: Session, item_id: int):
return db.query(models.Item).filter(models.Item.id == item_id).first()

def get_items(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.Item).offset(skip).limit(limit).all()

def create_item(db: Session, item: schemas.ItemCreate):
db_item = models.Item(title=item.title, description=item.description)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item

def delete_item(db: Session, item_id: int):
item = db.query(models.Item).filter(models.Item.id == item_id).first()
if item:
db.delete(item)
db.commit()
return item

def update_item(db: Session, item_id: int, item_data: schemas.ItemCreate):
item = db.query(models.Item).filter(models.Item.id == item_id).first()
if item:
item.title = item_data.title
item.description = item_data.description
db.commit()
db.refresh(item)
return item












🚀 Step 6: FastAPI Main App – main.py






# app/main.py
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session
from . import models, schemas, crud
from .database import SessionLocal, engine, Base

Base.metadata.create_all(bind=engine)

app = FastAPI()

# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

@app.post("/items/", response_model=schemas.Item)
def create_item(item: schemas.ItemCreate, db: Session = Depends(get_db)):
return crud.create_item(db=db, item=item)

@app.get("/items/", response_model=list[schemas.Item])
def read_items(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
return crud.get_items(db, skip=skip, limit=limit)

@app.get("/items/{item_id}", response_model=schemas.Item)
def read_item(item_id: int, db: Session = Depends(get_db)):
db_item = crud.get_item(db, item_id=item_id)
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found")
return db_item

@app.put("/items/{item_id}", response_model=schemas.Item)
def update_item(item_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)):
updated = crud.update_item(db, item_id, item)
if updated is None:
raise HTTPException(status_code=404, detail="Item not found")
return updated

@app.delete("/items/{item_id}", response_model=schemas.Item)
def delete_item(item_id: int, db: Session = Depends(get_db)):
deleted = crud.delete_item(db, item_id)
if deleted is None:
raise HTTPException(status_code=404, detail="Item not found")
return deleted












🧪 Step 7: Run and Test



Run the app:




uvicorn app.main:app --reload






Navigate to:



📚 Swagger UI: http://127.0.0.1:8000/docs



🧾 Redoc UI: http://127.0.0.1:8000/redoc









🧼 Optional Enhancements




  • Add authentication (JWT)

  • Migrate to PostgreSQL/MySQL

  • Integrate Alembic for migrations

  • Use FastAPI Users for user management









📝 Conclusion



FastAPI makes it incredibly easy to build powerful APIs with Python. This CRUD app demonstrated how to set up models, schemas, database interaction, and API routes.



Want to go further? Add frontend integration with React or Vue, deploy with Docker, or move to production with Gunicorn + Uvicorn!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 🚀 Building a CRUD Application with FastAPI – A Complete Guide

Thematisch verwandte Begriffe: Building, CRUD, Application, with · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96258 | A vulnerability has been found in onSite internet GmbH Auktion NG Auktio…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick