# Building a RESTful Express TypeScript API with Prisma and Authentication

In this blog post, I’ll guide you through building a RESTful API using **Express.js** and **TypeScript**, along with **Prisma ORM** for database operations and **JWT** for user authentication. This API includes **CRUD operations** for posts and user authentication functionalities, ensuring secure access through JWT-based authentication.

## 🚀 Features

This project has the following features:

* 👤 **User Registration and Login**: Secure user authentication.
    
* 🔐 **JWT-based Authentication**: Protect routes by verifying JWT tokens.
    
* 📝 **CRUD Operations for Posts**: Create, Read, Update, and Delete posts.
    
* 🐘 **PostgreSQL Database with Prisma ORM**: Database operations are handled by Prisma ORM.
    
* 📚 **Swagger API Documentation**: Automatically generated API documentation using Swagger.
    
* 🚨 **Comprehensive Error Handling and Logging**: Built-in error handling and logging functionality.
    
* 🌐 **CORS Support**: Handle cross-origin resource sharing.
    

---

## 📋 Prerequisites

Before you begin, ensure you have the following installed on your system:

* [Node.js](https://nodejs.org/) (v14 or later)
    
* npm or [yarn](https://yarnpkg.com/)
    
* PostgreSQL database (We’re using [Neon Postgres](https://neon.tech/))
    

---

## 🛠 Installation

### 1\. Clone the repository:

```bash
git clone https://github.com/azharhussain/Express-TypeScript-API-with-Prisma-and-Authentication.git
cd Express-TypeScript-API-with-Prisma-and-Authentication
```

### 2\. Install the dependencies:

```bash
npm install
```

### 3\. Set up your environment variables:

Create a `.env` file in the root directory and add the following:

```bash
DATABASE_URL="your_postgresql_connection_string"
JWT_SECRET="your_jwt_secret"
PORT=1000
```

### 4\. Set up the database:

Run the Prisma migration command to initialize the database schema:

```bash
npx prisma migrate dev --name init
```

---

## 🚀 Usage

To start the development server, run:

```bash
npm run dev
```

The server will start at [http://localhost:1000](http://localhost:1000) (or any port you’ve specified in the `.env` file).

---

## 📚 API Documentation

Once the server is running, you can access the **Swagger API documentation** by navigating to: [http://localhost:1000/api-docs](http://localhost:1000/api-docs)

---

## 🛠 API Endpoints

### **Authentication Routes:**

* **POST /api/auth/register**: Register a new user.
    
* **POST /api/auth/login**: Login an existing user.
    

### **Post Routes:**

* **GET /api/posts**: Get all posts.
    
* **GET /api/posts/:id**: Get a specific post by ID.
    
* **POST /api/posts**: Create a new post (requires authentication).
    
* **PUT /api/posts/:id**: Update an existing post (requires authentication).
    
* **DELETE /api/posts/:id**: Delete a post (requires authentication).
    

---

## 🔍 Testing

To test the API endpoints, you can use tools like **Postman** or **Insomnia**. A Postman collection is also provided in the repository to make testing easier.

### Steps to Test:

1. Import the Postman collection into your Postman workspace.
    
2. After logging in, update the `authToken` variable in Postman with a valid JWT token.
    
3. Test the available API endpoints.
    

---

## 📝 Logging

Logging is handled with `pino`. The logs are written to two files:

* **error.log**: Captures error logs.
    
* **combined.log**: Logs all activities.
    

In development mode, logs are also printed to the console for easier debugging.

---

## 👥 Contributing

Contributions are welcome! If you’d like to improve the project, follow these steps:

1. **Fork** the repository.
    
2. Create a new branch:
    
    ```bash
    git checkout -b feature/your-feature-name
    ```
    
3. Make your changes.
    
4. **Commit** the changes:
    
    ```bash
    git commit -am 'Add some feature'
    ```
    
5. Push to the branch:
    
    ```bash
    git push origin feature/your-feature-name
    ```
    
6. Create a new **Pull Request**.
    

---

## 📜 License

This project is licensed under the **MIT License**. Feel free to use and modify it as per your requirements.

---

## 📞 Contact

If you have any questions or feedback, feel free to reach out to me:

* **Website**: [Azhar Hussain](https://www.azharhussain.net)
    
* **Email**: [azharfastian@gmail.com](mailto:azharfastian@gmail.com)
    

---

By following this guide, you’ll have a robust RESTful API with Express, TypeScript, Prisma, and JWT authentication ready to go. Happy coding!
