Your first read-only SDK call
On this page
A small console app that reads one page of leads with ClientCraft.Sdk, prints counts only and handles errors without printing secrets.
Plan: All plans. Needs an integration key with CRM read permission
This is the quickest way to prove a connection works: read five leads and print how many you can see. It changes nothing in the workspace.
Before you start #
- The .NET 10 SDK.
- A workspace id and integration key with CRM read permission, from an approved connection.
- Create a new console project for this. Don't replace the
Program.csof an existing Umbraco site.
Step by step #
- 1Create the project fileSave this as
SdkProbe.csprojin an empty folder:1<Project Sdk="Microsoft.NET.Sdk">2 <PropertyGroup>3 <OutputType>Exe</OutputType>4 <TargetFramework>net10.0</TargetFramework>5 <ImplicitUsings>enable</ImplicitUsings>6 <Nullable>enable</Nullable>7 </PropertyGroup>8 <ItemGroup>9 <PackageReference Include="ClientCraft.Sdk" Version="1.0.0-preview.19" />10 <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.0" />11 <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />12 </ItemGroup>13</Project> - 2Add Program.cs1using ClientCraft.Sdk;2using ClientCraft.Sdk.Crm;3using Microsoft.Extensions.Configuration;4using Microsoft.Extensions.DependencyInjection;56var configuration = new ConfigurationBuilder()7 .AddEnvironmentVariables("CLIENTCRAFT_")8 .Build();910try11{12 using var services = new ServiceCollection()13 .AddClientCraft(configuration.GetSection("uCRM"))14 .BuildServiceProvider();1516 var client = services.GetRequiredService<IClientCraftClient>();1718 // Optional, from trusted server configuration. Selecting a workspace never grants access to it.19 var workspace = configuration["TargetWorkspace"];20 if (!string.IsNullOrWhiteSpace(workspace))21 client = client.ForWorkspace(workspace);2223 var result = await client.Crm.Leads.ListAsync(new CrmQuery(PageSize: 5));24 Console.WriteLine($"Read succeeded: {result.Items.Count} of {result.Total} accessible leads.");25 return 0;26}27catch (ClientCraftConfigurationException)28{29 Console.Error.WriteLine("Configuration invalid. Check the server-side connection settings.");30 return 2;31}32catch (ClientCraftApiException exception)33{34 Console.Error.WriteLine($"Read failed: {exception.Code}; correlation: {exception.CorrelationId ?? "unavailable"}.");35 return 3;36}
- 3Set the configurationSet these environment variables from your secret manager (double underscores mean nesting):1CLIENTCRAFT_uCRM__TenantId=<workspace id>2CLIENTCRAFT_uCRM__IntegrationKey=<integration key>3CLIENTCRAFT_uCRM__CrmApiBaseUrl=https://api.client-craft.com/
CLIENTCRAFT_TargetWorkspaceis optional. - 4Run it1dotnet run --project SdkProbe.csproj
What the result means #
| Exit code | Meaning | What to do |
|---|---|---|
| 0 | The read worked. An empty result is valid | Nothing |
| 2 | Configuration is missing or invalid | Check the three settings. The key must be printable ASCII, the URL must be HTTPS |
| 3 | The API refused or failed | Keep the code and correlation id. Fix the connection or permission through the supported flow; never switch to another workspace or credential to make it pass |
WarningNever put a real key in source control, this article's code or a browser.
What happens next #
Was this article helpful?