Client CraftClient Craft HubStart free

Your first read-only SDK call

On this page
Updated 24 Nov 20262 min read

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.cs of an existing Umbraco site.

Step by step #

  1. 1
    Create the project fileSave this as SdkProbe.csproj in an empty folder:
    xml
    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>
  2. 2
    Add Program.cs
    csharp
    1using ClientCraft.Sdk;
    2using ClientCraft.Sdk.Crm;
    3using Microsoft.Extensions.Configuration;
    4using Microsoft.Extensions.DependencyInjection;
    5
    6var configuration = new ConfigurationBuilder()
    7 .AddEnvironmentVariables("CLIENTCRAFT_")
    8 .Build();
    9
    10try
    11{
    12 using var services = new ServiceCollection()
    13 .AddClientCraft(configuration.GetSection("uCRM"))
    14 .BuildServiceProvider();
    15
    16 var client = services.GetRequiredService<IClientCraftClient>();
    17
    18 // 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);
    22
    23 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}
  3. 3
    Set the configurationSet these environment variables from your secret manager (double underscores mean nesting):
    bash
    1CLIENTCRAFT_uCRM__TenantId=<workspace id>
    2CLIENTCRAFT_uCRM__IntegrationKey=<integration key>
    3CLIENTCRAFT_uCRM__CrmApiBaseUrl=https://api.client-craft.com/

    CLIENTCRAFT_TargetWorkspace is optional.

  4. 4
    Run it
    bash
    1dotnet run --project SdkProbe.csproj

What the result means #

Exit codeMeaningWhat to do
0The read worked. An empty result is validNothing
2Configuration is missing or invalidCheck the three settings. The key must be printable ASCII, the URL must be HTTPS
3The API refused or failedKeep 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 #

SDK resources: clients, paging and errors.

Was this article helpful?