Skip to main content

Overview

The createDodoCustomer function creates a new customer record in Dodo Payments. This function is typically called during user registration to set up payment capabilities.

Function Signature

export async function createDodoCustomer(props: {
  email: string;
  name?: string;
}): ServerActionRes<Customer>

Parameters

email
string
required
The customer’s email address. Used as the primary identifier and for sending invoices.
name
string
The customer’s full name. If not provided, defaults to the username portion of the email (before @).

Return Value

success
boolean
required
Indicates whether the operation was successful
data
Customer
The created Dodo Payments customer object containing:
customer_id
string
Unique customer identifier in Dodo Payments
email
string
Customer’s email address
name
string
Customer’s name
And other Dodo Payments Customer fields…
error
string
Error message if the operation failed:
  • “Failed to create customer” - Customer creation in Dodo Payments failed

Implementation Details

The function:
  1. Accepts email (required) and name (optional) parameters
  2. If name is not provided, extracts the username from the email (text before @)
  3. Calls the Dodo Payments API to create a new customer
  4. Returns the complete customer object on success
  5. Catches and handles any API errors

Error Handling

  • Returns { success: false, error: "Failed to create customer" } if the Dodo API call fails
  • All exceptions are caught and converted to structured error responses
  • Does not throw exceptions, making it safe to use in Server Components

Usage Example

import { createDodoCustomer } from '@/actions/create-dodo-customer';

export default async function OnboardingHandler() {
  const result = await createDodoCustomer({
    email: '[email protected]',
    name: 'John Doe'
  });

  if (!result.success) {
    console.error('Failed to create customer:', result.error);
    return;
  }

  const customer = result.data;
  console.log('Customer created:', customer.customer_id);
  
  // Store customer_id in your database
  // Associate with user record
}

Using with Default Name

import { createDodoCustomer } from '@/actions/create-dodo-customer';

// Name will default to "user" from "[email protected]"
const result = await createDodoCustomer({
  email: '[email protected]'
});

if (result.success) {
  console.log('Customer name:', result.data.name); // "user"
}

Client Component Example

'use client';

import { createDodoCustomer } from '@/actions/create-dodo-customer';
import { useState } from 'react';

export function CustomerSetupForm() {
  const [email, setEmail] = useState('');
  const [name, setName] = useState('');
  const [loading, setLoading] = useState(false);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setLoading(true);

    const result = await createDodoCustomer({ email, name });

    if (result.success) {
      alert('Customer created successfully!');
      console.log('Customer ID:', result.data.customer_id);
    } else {
      alert(`Error: ${result.error}`);
    }

    setLoading(false);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        placeholder="Email"
        required
      />
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
        placeholder="Name (optional)"
      />
      <button type="submit" disabled={loading}>
        {loading ? 'Creating...' : 'Create Customer'}
      </button>
    </form>
  );
}

Integration with User Creation

import { createDodoCustomer } from '@/actions/create-dodo-customer';
import { db } from '@/lib/drizzle/client';
import { users } from '@/lib/drizzle/schema';

export async function setupNewUser(supabaseUserId: string, email: string, name: string) {
  // First, create Dodo customer
  const customerResult = await createDodoCustomer({ email, name });

  if (!customerResult.success) {
    throw new Error('Failed to create payment customer');
  }

  // Then, create user record with customer ID
  await db.insert(users).values({
    supabaseUserId,
    dodoCustomerId: customerResult.data.customer_id,
    currentSubscriptionId: '',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  });

  return customerResult.data;
}
  • createUser - Creates a user record (calls this function internally)
  • getUser - Retrieves the authenticated user

Source Code

Location: actions/create-dodo-customer.ts:7

Build docs developers (and LLMs) love