Skip to main content

Command Palette

Search for a command to run...

Building a RESTful Express TypeScript API with Prisma and Authentication

Updated
โ€ข3 min read
Building a RESTful Express TypeScript API with Prisma and Authentication
A

Over 2 decades of software engineering experience, including over a decade in building, scaling and leading engineering teams.

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:


๐Ÿ›  Installation

1. Clone the repository:

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:

npm install

3. Set up your environment variables:

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

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:

npx prisma migrate dev --name init

๐Ÿš€ Usage

To start the development server, run:

npm run dev

The server will start at 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


๐Ÿ›  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:

     git checkout -b feature/your-feature-name
    
  3. Make your changes.

  4. Commit the changes:

     git commit -am 'Add some feature'
    
  5. Push to the branch:

     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:


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

I
Issy1y ago

Thanks for always providing such great information on RESTful APIs! Iโ€™ve found EchoAPI to be a great addition to my toolkit, making it easy to simulate different API states and test without backend dependencies.

1
A

Thanks for sharing your feedback and I will definitely explore this

A

Great Article

1
G
Genie1y ago

How can we build same with Javascript

1
P

Worked like charm

1
U

Good share

1
S

Can you share the authentication service integrated with authjs?

1
T
Techveze1y ago

Extremely helpful

1

More from this blog

A

Azhar Hussain

44 posts

Experienced technology professional working with Engineering and Technology teams for over 2 decades