Skip to content

Commerce Customer API Reference

advanced
📜Corecommerce

The Customer API manages contacts, organizations, and addresses. CustomerContext provides the current customer. IOrganizationService and ICustomerContactService handle B2B hierarchies.

Optimizely.Commerce.Customers

TypePurpose
CustomerContextResolves the current customer contact
CustomerContactRepresents an individual customer
OrganizationRepresents a B2B organization
CustomerAddressShipping or billing address

Returns the CustomerContact for the current authenticated user.

Returns: CustomerContact or null if anonymous

Loads a customer contact by GUID.

Parameters

NameTypeDescription
contactIdGuidCustomer contact GUID

Returns: CustomerContact or null

Get current customer
csharp
public class CustomerService
{
  private readonly CustomerContext _customerContext;

  public CustomerService(CustomerContext customerContext)
  {
      _customerContext = customerContext;
  }

  public CustomerContact GetCurrentCustomer()
  {
      return _customerContext.CurrentContact;
  }

  public CustomerContact GetCustomerById(Guid id)
  {
      return _customerContext.GetContactById(id);
  }
}
PropertyTypeDescription
PrimaryKeyIdPrimaryKeyIdInternal primary key
UserIdstringMapped user identity
FirstNamestringFirst name
LastNamestringLast name
EmailstringEmail address
FullNamestringComputed full name
OwnerIdPrimaryKeyId?Parent organization ID
ContactAddressesIEnumerable<CustomerAddress>Associated addresses
PreferredBillingAddressCustomerAddressDefault billing address
PreferredShippingAddressCustomerAddressDefault shipping address
MethodReturnsDescription
SaveChanges()voidPersists modifications to the database
AddContactAddress(CustomerAddress)voidAdds a new address
UpdateContactAddress(CustomerAddress)voidUpdates an existing address
DeleteContactAddress(CustomerAddress)voidRemoves an address
Create a new customer contact
csharp
public CustomerContact CreateCustomer(string email, string firstName, string lastName)
{
  var contact = CustomerContact.CreateInstance();
  contact.UserId = "String:" + email;
  contact.Email = email;
  contact.FirstName = firstName;
  contact.LastName = lastName;
  contact.SaveChanges();
  return contact;
}
PropertyTypeDescription
PrimaryKeyIdPrimaryKeyIdInternal primary key
NamestringOrganization name
OrgCustomerGroupstringCustomer pricing group
AddressesIEnumerable<CustomerAddress>Organization addresses
ChildOrganizationsIEnumerable<Organization>Sub-organizations
ContactsIEnumerable<CustomerContact>Member contacts
MethodReturnsDescription
SaveChanges()voidPersists modifications
AddContact(CustomerContact)voidAdds a contact to the organization
RemoveContact(CustomerContact)voidRemoves a contact
Create an organization with a contact
csharp
public Organization CreateOrganization(string name)
{
  var org = Organization.CreateInstance();
  org.Name = name;
  org.OrgCustomerGroup = "B2B";
  org.SaveChanges();
  return org;
}

public void AssignContactToOrg(CustomerContact contact, Organization org)
{
  contact.OwnerId = org.PrimaryKeyId;
  contact.SaveChanges();
  org.AddContact(contact);
  org.SaveChanges();
}
PropertyTypeDescription
AddressIdPrimaryKeyIdUnique address identifier
NamestringAddress label (e.g., “Home”, “Office”)
FirstNamestringRecipient first name
LastNamestringRecipient last name
Line1stringStreet address line 1
Line2stringStreet address line 2
CitystringCity
StatestringState or province
PostalCodestringZIP or postal code
CountryCodestringTwo-letter ISO country code
CountryNamestringCountry display name
DaytimePhoneNumberstringPhone number
AddressTypeCustomerAddressTypeEnumShipping, Billing, or Public
Add address to contact
csharp
public void AddShippingAddress(CustomerContact contact)
{
  var address = CustomerAddress.CreateInstance();
  address.Name = "Office";
  address.AddressType = CustomerAddressTypeEnum.Shipping;
  address.Line1 = "123 Commerce St";
  address.City = "New York";
  address.State = "NY";
  address.PostalCode = "10001";
  address.CountryCode = "US";
  address.CountryName = "United States";

  contact.AddContactAddress(address);
  contact.SaveChanges();
}
Organization (Parent)
├── Organization (Division A)
│ ├── CustomerContact (Buyer 1)
│ └── CustomerContact (Buyer 2)
└── Organization (Division B)
└── CustomerContact (Buyer 3)
ExceptionCause
EntityNotFoundExceptionContact or organization GUID not found
DuplicateKeyExceptionUserId already exists
ValidationExceptionRequired fields missing