Controllers

Products - Controller

List and retrieve `Product` models.

To implement the Products controller, you need to implement 2 API endpoints, we will use these endpoints to fetch products from your system.

Prerequisites

Product Model

A code that can be redeemed for a discount.

Required

Families - Controller

If you decided to implement products with families, you must implement a Families controller first.

Optional

SDK

If you are using the SDK, you can implement the Products controller by following the code example below. You don't need to get into the details of the API endpoints, the SDK will take care of that for you.

import { Integrator } from '@churnkey/sdk'
import { Context } from '../Context'
import { Product } from '../models/Product'
// import { Families } from './Families'

export const Products = Integrator.Products.config({
    ctx: Context,
    type: Integrator.Product.Type.Standalone,
    // type: Integrator.Product.Type.Family,
    // Families: Families,
    async retrieve(ctx, options) {
        const yourProduct = await ctx.db.findProduct(options.id)
        return new Product(yourProduct)
    },
    async list(ctx, options) {
        const yourProducts = await ctx.db.listProducts({
            limit: options.limit,
            offset: options.cursor // the value you pass as `next` below
        })
        return {
            data: yourProducts.map(product => new Product(product)),
            // pass the next cursor if there are more items
            next: yourProducts.length === options.limit ? offset + limit : undefined
        }
    }
})

Endpoints

Retrieve Required

GET /churnkey/products/:id

This endpoint fetches Product by its id. Usually, implementation will include finding a product in your database and mapping it to the Product model.

List Required

GET /churnkey/products

This endpoint fetches a list of products from your database. You should find products in your database (with pagination), map them to the Product model and return a paginated list.

Learn more about pagination.

Webhooks

Coming soon.