> For the complete documentation index, see [llms.txt](https://docs.patchmypc.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.patchmypc.com/patch-my-pc-cloud-api/sdk/authentication.md). Agents can also search and read this documentation through its [MCP server](https://docs.patchmypc.com/~gitbook/mcp).

# SDK authentication methods

SDK methods for authenticating with a personal access token, reading the session's company, signing out, and checking service health.

[Back to the Cloud Customer API overview](https://docs.patchmypc.com/patch-my-pc-cloud-api)

These methods authenticate the SDK with the Patch My PC Cloud Customer API, report which company later calls run against, and sign you out.

The SDK authenticates with a personal access token (PAT). Call [`AuthenticateWithPersonalAccessTokenAsync`](#authenticatewithpersonalaccesstokenasync) once, before any other method. It needs no user at the keyboard, so it suits services, scheduled jobs, and pipelines. You create the PAT in the Patch My PC portal; see [Create a personal access token](https://docs.patchmypc.com/patch-my-pc-cloud-api/create-a-personal-access-token.md).

Until you authenticate, most other methods fail with `VALIDATION_ERROR` because no company is selected. For an overview of how authentication works, see [Authentication](https://docs.patchmypc.com/patch-my-pc-cloud-api/authentication.md).

## Company model

`CompanyInfo` (namespace `PatchMyPC.Cloud.Models`) identifies the company a session works with.

| Property      | Type                          | Description                                                                                  |
| ------------- | ----------------------------- | -------------------------------------------------------------------------------------------- |
| `CustomerId`  | `string`                      | The company's customer ID.                                                                   |
| `CompanyName` | `string`                      | The company's display name.                                                                  |
| `RegionId`    | `Guid`                        | The region the company's data is hosted in.                                                  |
| `RegionUrls`  | `IDictionary<string, string>` | The regional addresses the SDK uses for this company. You don't normally need to read these. |
| `Metadata`    | `Dictionary<string, object>?` | Additional information about the company, if any.                                            |

## Methods

### AuthenticateWithPersonalAccessTokenAsync

Authenticates with a personal access token and selects the token's company, so the SDK is ready to use in one step.

```csharp
Task<Result<CompanyInfo>> AuthenticateWithPersonalAccessTokenAsync(
    string personalAccessToken,
    CancellationToken cancellationToken = default);
```

| Parameter             | Type                | Description                                                          |
| --------------------- | ------------------- | -------------------------------------------------------------------- |
| `personalAccessToken` | `string`            | Required. The full token value, as shown when the token was created. |
| `cancellationToken`   | `CancellationToken` | Optional. Cancels the call.                                          |

**Returns:** the [`CompanyInfo`](#company-model) of the company the token belongs to. Every later call runs against that company.

The SDK exchanges the PAT for an access token that's valid for 60 minutes, then exchanges the PAT again as the access token expires, so one authenticated `IPmpcCloudClient` instance works for a long-running job. Set `Region` (`Us`, `Eu`, or `Uk`) in the SDK configuration to use your account's regional host. If you omit it, the SDK uses the standard portal host. For details, see [Install and configure the SDK](https://docs.patchmypc.com/patch-my-pc-cloud-api/sdk/install-and-configure.md).

A PAT can only do what its scopes allow, and never more than the user who created it. The `appcatbroker:apps:deploy` scope covers both deployments and deployment templates. For the full list, see [Scopes](https://docs.patchmypc.com/patch-my-pc-cloud-api/authentication.md#scopes). Calls outside its scopes fail with `AUTHZ_ERROR`. Authentication itself can fail with these codes:

| Code                     | Meaning                                                           |
| ------------------------ | ----------------------------------------------------------------- |
| `AUTH_ERROR`             | The token is invalid, expired, or revoked.                        |
| `PAT_EXCHANGE_THROTTLED` | Too many token exchanges in a short time. Wait, then retry.       |
| `PAT_NOT_ENTITLED`       | The token's company isn't entitled to use personal access tokens. |

The usual pattern is to create a PAT once in the Patch My PC portal, store it in a secret store, and read it on every run:

```csharp
var token = Environment.GetEnvironmentVariable("PMPC_PAT")
    ?? throw new InvalidOperationException("Set PMPC_PAT to a personal access token.");

var session = await client.AuthenticateWithPersonalAccessTokenAsync(token);
if (session.IsFailure)
{
    Console.WriteLine($"Authentication failed: {session.Error.Code} {session.Error.Message}");
    return;
}

Console.WriteLine($"Connected to {session.Value.CompanyName}");
```

**See also:** [Connect-PmpcCloudWithPat](https://docs.patchmypc.com/patch-my-pc-cloud-api/powershell/connect-pmpccloudwithpat.md) and [Exchange a personal access token for an access token](https://docs.patchmypc.com/patch-my-pc-cloud-api/rest-api/authentication.md#exchange-a-personal-access-token-for-an-access-token).

### GetSelectedCompany

Returns the company the session works with.

```csharp
CompanyInfo? GetSelectedCompany();
```

This method has no parameters.

**Returns:** the [`CompanyInfo`](#company-model) of the token's company, or `null` if you haven't authenticated yet. Unlike the other methods, it returns the value directly rather than a result object.

```csharp
var current = client.GetSelectedCompany();
Console.WriteLine(current is null
    ? "Not authenticated."
    : $"Working with {current.CompanyName}");
```

**See also:** [Get-PmpcSelectedCompany](https://docs.patchmypc.com/patch-my-pc-cloud-api/powershell/get-pmpcselectedcompany.md). This method has no REST operation; it reads the company stored in the SDK.

### ClearAuthenticationAsync

Signs out by clearing the session's company and service configuration.

```csharp
Task<Result> ClearAuthenticationAsync();
```

This method has no parameters.

**Returns:** a `Result` that indicates success or failure.

After this call, methods that need a company fail until you authenticate again.

```csharp
var signOut = await client.ClearAuthenticationAsync();
if (signOut.IsFailure)
{
    Console.WriteLine($"Sign-out failed: {signOut.Error.Message}");
}
```

**See also:** [Disconnect-PmpcCloud](https://docs.patchmypc.com/patch-my-pc-cloud-api/powershell/disconnect-pmpccloud.md). This method has no REST operation.

### IsServiceHealthyAsync

Checks whether the Cloud Customer API responds to a heartbeat request for the session's company. This does not check every service dependency.

```csharp
Task<Result<bool>> IsServiceHealthyAsync(CancellationToken cancellationToken = default);
```

| Parameter           | Type                | Description                 |
| ------------------- | ------------------- | --------------------------- |
| `cancellationToken` | `CancellationToken` | Optional. Cancels the call. |

**Returns:** A successful `Result<bool>` containing `true` when a heartbeat response is received; otherwise `false`. A failed health check, including a network or token-acquisition failure, or a call with no selected company, returns `false` rather than a failed result. A false value does not distinguish these causes.

```csharp
var health = await client.IsServiceHealthyAsync();
if (health.IsSuccess && health.Value)
{
    Console.WriteLine("The Cloud Customer API is available.");
}
else
{
    Console.WriteLine("The Cloud Customer API isn't reachable. Check your network connection and that you've authenticated.");
}
```

**See also:** [Test-PmpcCloudHealth](https://docs.patchmypc.com/patch-my-pc-cloud-api/powershell/test-pmpccloudhealth.md). This method calls the service's health endpoint, which isn't part of the documented REST operations.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.patchmypc.com/patch-my-pc-cloud-api/sdk/authentication.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
