DEV Community

Cover image for Strapi API with PostgreSQL – Quick Reference πŸš€
Taufiqul Islam
Taufiqul Islam

Posted on • Edited on

Strapi API with PostgreSQL – Quick Reference πŸš€

Strapi is an open-source headless CMS (Content Management System) built to work with any frontend and any database. It provides developers with a flexible and customizable solution for building content-rich applications, giving them full control over the API, data model, and platform.

Why Strapi? πŸ—οΈ

Strapi sets itself apart from other CMS solutions in several ways:

Open-source and Self-hosted: Unlike most CMS platforms, Strapi allows you to self-host your instance, offering greater flexibility, control, and security.
Headless by Design: Strapi is built as a headless CMS, meaning the frontend and backend are decoupled, giving you the freedom to use any frontend technologyβ€”React, Vue, Angular, or even native mobile apps.
Customizable APIs: Strapi lets you customize the API for your specific needs, whether it’s REST or GraphQL, making it ideal for projects requiring complex data structures.
Database Agnostic: You can use Strapi with multiple databases like PostgreSQL, MongoDB, MySQL, or SQLite, offering seamless flexibility for different use cases.


This guide provides an overview of basic operations for managing products and images using Strapi, with PostgreSQL as the database. Ensure you have PostgreSQL and PgAdmin 4 installed if you're using PostgreSQL for your Strapi setup.

Prerequisites πŸ› οΈ

REST API Overview 🌳

For complete details, refer to the Strapi REST API documentation.


Products Collection API

Assume a Products collection with fields:

  • Product_name: String (Name of the product)
  • img: Media (Image ID associated with the product)
  • state: Boolean (Indicating product status)

1. Get All Products

Retrieve all products with their associated data (including images).

Endpoint:

GET http://localhost:1337/api/products?populate=*

  • populate=* ensures all relational data, including images, are properly fetched.

2. Upload an Image

Upload an image to the Strapi server.

Endpoint:

POST http://localhost:1337/api/upload/

Request Body: (form-data)

  • key = files (value is the image file uploaded from your browser).

3. Get All Uploaded Images

Retrieve all uploaded image files.

Endpoint:

GET http://localhost:1337/api/upload/files

4. Add a New Product

Create a new product entry in the Products collection.

Endpoint:

POST http://localhost:1337/api/products

Request Body:

{
    "data": {
        "Product_name": "Samsung S4 old colored",
        "state": true,
        "img": 3 
    }
}
Enter fullscreen mode Exit fullscreen mode
  • The "img": 3 field refers to the ID of the uploaded image, in this case, image ID 3.

5. Update a Product

Update a specific product using its documentId.

Endpoint:

PUT http://localhost:1337/api/products/:documentId

Request Body:

{
    "data": {
        "Product_name": "Samsung S4 old updated",
        "state": true,
        "img": 3 
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, you update the product name while keeping the image ID (3), which represents the already uploaded image.


πŸ“š This document should serve as a quick reference for managing products and images in Strapi with PostgreSQL. For further customization or detailed configurations, consult the official Strapi documentation.

Top comments (0)