Create a customer

This guide describes how to create a customer using the Rebilly JS SDK, Express JS, and the Upsert a customer API operation.

Prerequisites

To complete this guide, you must have a website ID, an organization ID, and a secret API key.

If you already have your IDs and API keys, continue to Step 1: Set up the server. If you do not, get your IDs and API key in Set up your environment.

Step 1: Set up the server

This section describes how to set up your server-side code to create customers using the Rebilly JS SDK.

Install and initialize dependencies

  1. Install the required dependencies in your project directory:

    npm install express rebilly-js-sdk@latest --save
  2. Initialize Express and the Rebilly SDK as dependencies.

Provide credentials

Provide your secret key, website ID, and organization ID. For more information, see Prerequisites.

Store your secret key in a secrets manager, not in your code.

Initialize Rebilly JS SDK

Set up the Rebilly JS SDK and provide your secret API key. Set sandbox to true to test in the sandbox environment.

Define route for POST requests

Define a route for handling HTTP POST requests to the /create-customer endpoint.

This endpoint uses the Rebilly SDK customers.upsert() method to create or update customers.

Build customer data structure

Create the customer data object with the required structure for the Rebilly API, including the customer ID, website ID, and primary address information.

Call the upsert API

Use the Rebilly SDK customers.upsert() method to create or update the customer. This method creates a new customer if the ID does not exist, or updates an existing customer if it does.

Handle errors

Implement error handling to catch and respond to any errors that occur during customer creation. This includes API errors, validation errors, and network issues.

The error response includes the HTTP status code and detailed error information from the Rebilly API.

Step 2: Set up the form

This section describes how to create a form that collects customer information and simulates a customer registration or authentication. Customer information is sent to the server to create or update a customer.

Create HTML form

Create an HTML form with fields for customer information:

  • Customer ID (required) - Enter your external identifier for the customer. If the ID already exists in Rebilly, the API updates the customer's information. If the ID does not exist, a new customer is created.
  • First name (required)
  • Last name (required)
  • Email (required)
  • Phone (optional)

You can also provide other fields that are supported by the Upsert a customer API operation, such as address and date of birth.

Add JavaScript handler

Add JavaScript to handle form submission and send the data to your server endpoint.

Step 3: Test the customer creation

This section describes how to test the customer creation functionality.

Run the example

Run server.js. When the server is running, open a browser and visit http://localhost:8080.

node server.js
import express from "express";
import RebillyAPI from "rebilly-js-sdk";

const app = express();

app.use(express.json());

const API_SECRET_KEY = "SECRET_KEY";
const ORGANIZATION_ID = "ORGANIZATION_ID";
const WEBSITE_ID = "WEBSITE_ID";

const rebilly = RebillyAPI({
    organizationId: ORGANIZATION_ID,
    sandbox: true,
    apiKey: API_SECRET_KEY,
    timeout: 5000,
});

app.use(express.static("public"));

app.post("/create-customer", async (req, res) => {
    try {
        const { customerId, firstName, lastName, email, phone } = req.body;
        const customerData = {
            data: {
                id: customerId,
                websiteId: WEBSITE_ID,
                primaryAddress: {
                    firstName,
                    lastName,
                    emails: [{ label: "primary", value: email, primary: true }],
                    ...(phone && {
                        phoneNumbers: [{ label: "primary", value: phone, primary: true }],
                    }),
                },
            },
        };
        const response = await rebilly.customers.upsert(customerData);
        res.status(200).json({ success: true, customer: response.fields });
    } catch (error) {
        console.error("Error creating customer:", error);
        res.status(error?.response?.status || 500).json({
            error: "Failed to create customer",
            details: error?.response?.data || error.message,
        });
    }
});

app.listen(8080, () => console.log("Running on port 8080"));