Client CraftClient Craft HubStart free

SDK resources: clients, paging and errors

On this page
Updated 24 Nov 20264 min read

Every resource client in the Client Craft .NET SDK, with its methods, page-based listing, versioned writes, workspace selection and the exceptions it throws.

Plan: All plans. Needs an integration key

A reference for working with IClientCraftClient after you have registered it. The API still decides every authorization, quota and replay; the SDK makes those rules hard to get wrong.

Before you start #

How it works #

  1. 1
    Find the client you need
    PropertyPurposeAPI collection
    client.Crm.LeadsLead records, plus linking a company/api/v1/crm/leads
    client.Crm.ContactsContact records/api/v1/crm/contacts
    client.Crm.CompaniesCompany records/api/v1/crm/accounts
    client.Crm.OperationsSummaries, duplicate checks, saved view statevarious
    client.TenancyConnection, current workspace, capabilities, directory, membershipsvarious
    client.WebsiteAnalytics()Optional server-side observation (extension method in ClientCraft.Sdk.Analytics)various

    CRM covers leads, contacts and companies. Deals, forms, bookings and imports have no SDK client yet.

  2. 2
    Use the factory for special connectionsIClientCraftClientFactory.Create(connection, actorId) builds a client for an explicit connection and a verified actor.
  3. 3
    Call the record methodsLeads, contacts and companies share these methods (all take an optional CancellationToken):
    MethodReturns
    ListAsync(CrmQuery? query)CrmPage<TRecord, TSummary>
    GetAsync(int id)the record
    CreateAsync(input, string idempotencyKey)the new record
    UpdateAsync(int id, input, Guid version)the updated record
    ArchiveAsync(int id, Guid version)RecordLifecycleResult
    RestoreAsync(int id, Guid version, string idempotencyKey)RecordLifecycleResult

    Leads add SetCompanyAsync(int id, int? accountId, Guid version) to link or clear a company.

  4. 4
    Page through listsListing is page-based. CrmQuery takes Page (from 1), PageSize (1 to 100, default 20), Search, Source, OwnerId, TeamId, IncludeArchived and Sort. CrmPage returns Items, Total, Page, PageSize, a Summary and Facets.
    csharp
    1using ClientCraft.Sdk;
    2using ClientCraft.Sdk.Crm;
    3
    4public sealed class LeadReader(IClientCraftClient client)
    5{
    6 public async Task ReadAllAsync(string workspaceId, CancellationToken ct)
    7 {
    8 // Select an authorized workspace; the credential owner does not change.
    9 var leads = client.ForWorkspace(workspaceId).Crm.Leads;
    10 var page = 1;
    11 while (true)
    12 {
    13 var result = await leads.ListAsync(new CrmQuery(Page: page, PageSize: 100), ct);
    14 foreach (var lead in result.Items)
    15 Console.WriteLine($"{lead.Id} {lead.Status}");
    16 // Stop after the last page.
    17 if (result.Page * result.PageSize >= result.Total) break;
    18 page++;
    19 }
    20 }
    21}
  5. 5
    Write with keys and versionsCreate and restore need a stable idempotency key of up to 256 visible ASCII characters, sent as Idempotency-Key. Reuse the same key when you retry after a lost response. Update, archive and restore need the record's Version from the API, sent as If-Match. UpdateAsync is a full-record PUT: send every editable value you want to keep, not only the changed ones.
  6. 6
    Target a workspaceForWorkspace(tenantId) returns a client for another workspace with the same credential and actor. It grants nothing; the API checks the credential, hierarchy and permissions. An empty or invalid id throws ArgumentException before any request is sent.
  7. 7
    Use CRM operationsclient.Crm.Operations offers GetAttentionSummaryAsync, GetCollectionSummaryAsync, FindLeadDuplicatesAsync, FindContactDuplicatesAsync, FindCompanyDuplicatesAsync, GetViewPreferenceAsync and SaveViewPreferenceAsync. Duplicate results are suggestions; the SDK never picks or merges records for you. A saved view that doesn't exist returns 404, which is different from an access denial. Preference updates are sent once, never retried.
  8. 8
    Handle errorsAPI failures throw ClientCraftApiException with StatusCode, Code, CorrelationId, ValidationErrors and ResponseWasEmpty. The message never contains the response body. Local codes include request_timeout (504), connection_unavailable (503) and invalid_response (502). Configuration problems throw ClientCraftConfigurationException.
    csharp
    1using ClientCraft.Sdk;
    2using Microsoft.Extensions.Logging;
    3
    4public sealed class LeadArchiver(IClientCraftClient client, ILogger<LeadArchiver> logger)
    5{
    6 public async Task ArchiveAsync(int leadId, CancellationToken ct)
    7 {
    8 try
    9 {
    10 var lead = await client.Crm.Leads.GetAsync(leadId, ct);
    11 // Archive with the version you just read.
    12 var archived = await client.Crm.Leads.ArchiveAsync(lead.Id, lead.Version, ct);
    13 logger.LogInformation("{Type} {Id} archived at {At}", archived.EntityType, archived.Id, archived.ArchivedAtUtc);
    14 }
    15 catch (ClientCraftApiException ex)
    16 {
    17 // Log the safe metadata; the response body is not exposed.
    18 logger.LogWarning("Client Craft {Status} {Code} {CorrelationId}", (int)ex.StatusCode, ex.Code, ex.CorrelationId);
    19 foreach (var (field, messages) in ex.ValidationErrors)
    20 logger.LogWarning("{Field}: {Messages}", field, string.Join(" ", messages));
    21 }
    22 }
    23}

    The SDK doesn't retry writes. The default timeout is 30 seconds and can be raised to five minutes with Timeout.

TipAfter a request_timeout on a create, retry with the same idempotency key: the API replays the first result instead of creating a duplicate. If an update or archive is refused because the record changed, read it again, review the new values and send the new Version. Quote the CorrelationId when you contact support.

What happens next #

Authentication, permissions and workspace scope.

Was this article helpful?