SDK resources: clients, paging and errors
On this page
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 #
- The SDK registered and
IClientCraftClientinjected, as in Your first read-only SDK call.
How it works #
- 1Find the client you need
Property Purpose API collection client.Crm.LeadsLead records, plus linking a company /api/v1/crm/leadsclient.Crm.ContactsContact records /api/v1/crm/contactsclient.Crm.CompaniesCompany records /api/v1/crm/accountsclient.Crm.OperationsSummaries, duplicate checks, saved view state various client.TenancyConnection, current workspace, capabilities, directory, memberships various 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.
- 2Use the factory for special connections
IClientCraftClientFactory.Create(connection, actorId)builds a client for an explicit connection and a verified actor. - 3Call the record methodsLeads, contacts and companies share these methods (all take an optional
CancellationToken):Method Returns 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)RecordLifecycleResultRestoreAsync(int id, Guid version, string idempotencyKey)RecordLifecycleResultLeads add
SetCompanyAsync(int id, int? accountId, Guid version)to link or clear a company. - 4Page through listsListing is page-based.
CrmQuerytakesPage(from 1),PageSize(1 to 100, default 20),Search,Source,OwnerId,TeamId,IncludeArchivedandSort.CrmPagereturnsItems,Total,Page,PageSize, aSummaryandFacets.1using ClientCraft.Sdk;2using ClientCraft.Sdk.Crm;34public 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} - 5Write 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'sVersionfrom the API, sent asIf-Match.UpdateAsyncis a full-record PUT: send every editable value you want to keep, not only the changed ones. - 6Target a workspace
ForWorkspace(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 throwsArgumentExceptionbefore any request is sent. - 7Use CRM operations
client.Crm.OperationsoffersGetAttentionSummaryAsync,GetCollectionSummaryAsync,FindLeadDuplicatesAsync,FindContactDuplicatesAsync,FindCompanyDuplicatesAsync,GetViewPreferenceAsyncandSaveViewPreferenceAsync. 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. - 8Handle errorsAPI failures throw
ClientCraftApiExceptionwithStatusCode,Code,CorrelationId,ValidationErrorsandResponseWasEmpty. The message never contains the response body. Local codes includerequest_timeout(504),connection_unavailable(503) andinvalid_response(502). Configuration problems throwClientCraftConfigurationException.1using ClientCraft.Sdk;2using Microsoft.Extensions.Logging;34public sealed class LeadArchiver(IClientCraftClient client, ILogger<LeadArchiver> logger)5{6 public async Task ArchiveAsync(int leadId, CancellationToken ct)7 {8 try9 {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 #
Was this article helpful?