Skip to main content

Products API

Product Management

Create and retrieve products in the recommendation system.

Create Products

Upload multiple products at once for a customer.

Endpoint

POST /api/products/upload

Authentication

Required: JWT Bearer token

Request Body

[
{
"customer_id": "customer_123",
"name": "Wireless Headphones",
"description": "High-quality Bluetooth headphones with noise cancellation",
"price": 199.99
},
{
"customer_id": "customer_123",
"name": "Smart Watch",
"description": "Fitness tracking and notifications",
"price": 299.99
}
]

Request Fields

FieldTypeRequiredDescription
customer_idstringYesCustomer identifier (must match authenticated user)
namestringYesProduct name
descriptionstringNoProduct description
pricefloatYesProduct price

Response

{
"product_ids": [
"64a1b2c3d4e5f6789012345",
"64a1b2c3d4e5f6789012346"
]
}

Error Responses

400 Bad Request

{
"detail": "Product customer_id customer_456 does not match customer_id customer_123"
}

400 Bad Request

{
"detail": "Error creating products: Database connection failed"
}

Get Products

Retrieve products by ID or get all products for the authenticated customer.

Endpoint

GET /api/products/get

Authentication

Required: JWT Bearer token

Query Parameters

ParameterTypeRequiredDefaultDescription
product_idstringNo-Specific product ID to retrieve
limitintegerNo100Maximum number of products to return

Response

[
{
"product_id": "64a1b2c3d4e5f6789012345",
"customer_id": "customer_123",
"name": "Wireless Headphones",
"description": "High-quality Bluetooth headphones with noise cancellation",
"price": 199.99,
"created_at": "2023-07-01T14:30:00.000Z"
}
]

Response Fields

FieldTypeDescription
product_idstringUnique product identifier
customer_idstringCustomer who owns the product
namestringProduct name
descriptionstringProduct description (if provided)
pricefloatProduct price
created_atstringISO timestamp of creation

Error Responses

404 Not Found

{
"detail": "Product not found"
}

500 Internal Server Error

{
"detail": "Database connection error"
}

Example Usage

Create Products

curl -X POST "http://localhost:5051/api/products/upload" \
-H "Authorization: Bearer <jwt-token>" \
-H "Content-Type: application/json" \
-d '[
{
"customer_id": "customer_123",
"name": "Laptop Stand",
"description": "Adjustable aluminum laptop stand",
"price": 49.99
}
]'

Get All Products

curl -X GET "http://localhost:5051/api/products/get" \
-H "Authorization: Bearer <jwt-token>"

Get Specific Product

curl -X GET "http://localhost:5051/api/products/get?product_id=64a1b2c3d4e5f6789012345" \
-H "Authorization: Bearer <jwt-token>"

Data Storage

Products are stored in MongoDB with the following structure:

  • Collection: products
  • Indexing: Automatic indexing on product_id and customer_id
  • Metadata: Each product includes creation timestamp and batch information

Security Notes

  • Products can only be created for the authenticated customer's ID
  • All product operations are logged for audit purposes
  • MongoDB connection uses secure connection strings from environment variables