DEV Community

Surender Gupta
Surender Gupta

Posted on

1

🧠 Build an AI Safety Incident Log API with Node.js, Express & MongoDB

As AI becomes more critical in decision-making systems, tracking AI safety incidents is essential. Here’s a simple RESTful API to log, retrieve, and manage such incidents using Node.js, Express, and MongoDB.

Workflow of Api

πŸ”§ Features

  • Log incidents with title, description, and severity.
  • Retrieve all or specific incidents.
  • Delete incidents.
  • Input validation with Joi.
  • MongoDB (Mongoose) for data storage.
  • Custom error handling.
  • CORS support.

πŸš€ Quick Start

1. Clone & Install

git clone https://github.com/surendergupta/AI-Safety-Incident-Log-Service.git
cd AI-Safety-Incident-Log-Service
npm install
Enter fullscreen mode Exit fullscreen mode

Install required packages:

npm i cors dotenv express http-status joi mongoose path
npm i nodemon --save-dev
Enter fullscreen mode Exit fullscreen mode

Installed packages

2. Setup .env

PORT=5000
MONGO_URI=mongodb://localhost:27017/ai_safety_log_api_db
NODE_ENV=development
Enter fullscreen mode Exit fullscreen mode

3. Run the Server

npm run dev
Enter fullscreen mode Exit fullscreen mode

Server will be live at: http://localhost:5000

πŸ” Endpoints

βœ… GET /incidents

Get all logged incidents.

πŸ“ POST /incidents

{
  "title": "System Crash",
  "description": "AI failed due to memory leak.",
  "severity": "High"
}
Enter fullscreen mode Exit fullscreen mode

Output

Incident Create

πŸ” GET /incidents/:id

❌ DELETE /incidents/:id

🧠 Schema

const incidentSchema = new mongoose.Schema({
  title: String,
  description: String,
  severity: { type: String, enum: ["Low", "Medium", "High"] }
}, { timestamps: true });
Enter fullscreen mode Exit fullscreen mode

βœ… Why This Project?

  • 🚦 Demonstrates validation with Joi.
  • 🚫 Handles errors cleanly.
  • πŸ“¦ Uses scalable document storage (MongoDB).
  • πŸ§ͺ Great for API testing, learning, or as a boilerplate.

Check it out πŸ‘‰

AI Safety Incident Log Service

Overview

This is a simple RESTful API service built with Node.js, Express.js, and MongoDB to log and manage AI safety incidents.

Flow of the API

Backend Flow Diagram

Features

  • Create, retrieve, and delete AI safety incidents.
  • Uses MongoDB for persistent storage.
  • MongoDB as the database with Mongoose ORM.
  • Request validation using Joi.
  • Implements basic validation and error handling.
  • API error handling with custom error classes.
  • Supports CORS for cross-origin requests.

Prerequisites

  • Node.js (>= 14.x recommended)
  • MongoDB (running locally or in the cloud, e.g., MongoDB Atlas)

Clone the Repository

git clone https://github.com/surendergupta/AI-Safety-Incident-Log-Service.git
cd AI-Safety-Incident-Log-Service
Enter fullscreen mode Exit fullscreen mode

Install Dependencies

npm install
Enter fullscreen mode Exit fullscreen mode

Install Packages

npm i cors
npm i dotenv --save
npm i express
npm i http-status
npm i joi
npm i mongoose
npm i nodemon --save-dev
npm i path
Enter fullscreen mode Exit fullscreen mode

npm list

Set Up Environment

Create a .env file in the project root with:

PORT=5000
MONGO_URI=mongodb://localhost:27017/ai_safety_log_api_db  # Update this if using
…
Enter fullscreen mode Exit fullscreen mode

✨ Thank you for reading! ✨ I hope this article helped simplify the process and gave you valuable insights. As I continue to explore the ever-evolving world of technology, I’m excited to share more guides, tips, and updates with you. πŸš€ Stay tuned for more content that breaks down complex concepts and makes them easier to grasp. Let’s keep learning and growing together! πŸ’‘

Found this useful? Give it a ❀️ or leave a comment with suggestions!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay