Controllers

Customers - Controller

List, retrieve and find `Customer` models.

To implement the Customers controller, you need to implement at least 2 API endpoints, we will use these endpoints to fetch customers from your system.

Prerequisites

Customer Model

A user who owns subscriptions.

Required

Coupons - Controller

If you have customer-level discounts, you may need to implement a Coupons controller first.

OptionalDiscount Offer

SDK

If you are using the SDK, you can implement the Customers 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 { Customer } from '../models/Customer'

export const Customers = Integrator.Customers.config({
    ctx: Context,
    async retrieve(ctx, options) {
        const yourCustomer = await ctx.db.findCustomerById(options.id)
        return new Customer(yourCustomer) 
    },
    async list(ctx, options) {
        const yourCustomers = await ctx.db.findCustomers({
            limit: options.limit,
            offset: options.cursor // the value you pass as `next` below
        })
        return {
            data: yourCustomers.map(c => new Customer(c)),
            // pass the next cursor if there are more items
            next: yourCustomers.length === options.limit ? offset + limit : undefined
        }
    },
    async findByEmail(ctx, email) { // optional
        const yourCustomer = await ctx.db.findCustomerByEmail(email)
        return new Customer(yourCustomer)
    },
    async findByPhone(ctx, phone) { // optional
        const yourCustomer = await ctx.db.findCustomerByPhone(phone)
        return new Customer(yourCustomer)
    }
})

Endpoints

Retrieve Required

GET /churnkey/customers/:id

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

List Optional

GET /churnkey/customers

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

Learn more about pagination.

Find by Email Managed Flow

GET /churnkey/customers/email/:email

This endpoint fetches Customer by its email. Usually, implementation will include finding a customer in your database and mapping it to the Customer model.

Make sure that each customer has a unique email address if you are going to implement this endpoint.

Find by Phone Managed Flow

GET /churnkey/customers/phone/:phone

This endpoint fetches Customer by its phone. Usually, implementation will include finding a customer in your database and mapping it to the Customer model.

Make sure that each customer has a unique phone number if you are going to implement this endpoint.

Webhooks

Coming soon.