---
title: "App Authentication with an API Key"
slug: "dsm-example-code-app-authentication-with-an-api-key"
updated: 2024-12-11T18:37:47Z
published: 2024-12-11T18:37:47Z
canonical: "support.fortanix.com/dsm-example-code-app-authentication-with-an-api-key"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://support.fortanix.com/llms.txt
> Use this file to discover all available pages before exploring further.

# App Authentication with an API Key

- [C#](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-1)
- [Go](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-2)
- [Java](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-3)
- [Python](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-4)
- [PHP](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-5)
- [Javascript](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-6)
- [REST API using curl](/docs/dsm-example-code-app-authentication-with-an-api-key#tabs-7)

```bash
Configuration.Default.BasePath = "<Endpoint URL>";
string decodedAPIKey = Encoding.ASCII.GetString(Convert.FromBase64String(<API Key>));
string[] decodeApiTokens = decodedAPIKey.Split(':');
Configuration.Default.Username = decodeApiTokens[0];
Configuration.Default.Password = decodeApiTokens[1];

AuthenticationApi authenticationApi = new AuthenticationApi();
AuthResponse response = authenticationApi.Authorize();
Configuration.Default.AddApiKey("Authorization", response.AccessToken);
Configuration.Default.AddApiKeyPrefix("Authorization", "Bearer");
```

### Go

```bash
client := sdkms.Client{
      Endpoint: "<Endpoint URL>",
      HTTPClient: http.DefaultClient,
}
ctx := context.Background()
_, err := client. AuthenticateWithAPIKey(ctx, <API Key>)
```

### Java

```bash
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(<Endpoint URL>);
apiClient.setBasicAuthString(<API Key>);
AuthenticationApi authenticationApi = new AuthenticationApi(apiClient);
AuthResponse authResponse = authenticationApi.authorize();
ApiKeyAuth bearerTokenAuth = (ApiKeyAuth) apiClient.getAuthentication("bearerToken");
bearerTokenAuth.setApiKey(authResponse.getAccessToken());
bearerTokenAuth.setApiKeyPrefix("Bearer");
```

### Python

```bash
config = sdkms.v1.Configuration()
config.host = "<Endpoint URL>"
config.app_api_key = <API Key>
client = sdkms.v1.ApiClient(configuration=config)
auth_instance = sdkms.v1.AuthenticationApi(api_client=client)
auth = auth_instance.authorize()
config.api_key['Authorization'] = auth.access_token
config.api_key_prefix['Authorization'] = 'Bearer'
```

### PHP

```bash
$configuration = new SdkmsClient\Configuration();
$decodedApiKey = base64_decode('<API Key>')
$decodedApiTokens = explode(":",$decodedApiKey);
$configuration->setUsername($decodedApiTokens[0]);
$configuration->setPassword($decodedApiTokens[1]);
$configuration->setHost('<Endpoint URL>');
$client = new SdkmsClient\ApiClient($configuration);
      
$api_instance = new SdkmsClient\Api\AuthenticationApi($client);
$auth_response = api_instance.authorize();
$configuration->setApiKey('Authorization', $auth_response->getAccessToken());
$configuration->setApiKeyPrefix('Authorization', 'Bearer');
$auth_response = api_instance.authorize();
```

### Javascript

```bash
var FortanixSdkmsRestApi = require('fortanix_sdkms_rest_api');
var client = FortanixSdkmsRestApi.ApiClient.instance;
client.basePath = '<Endpoint URL>';
let buff = new Buffer('<API Key>;', 'base64');
let decodedApiKey = buff.toString('ascii');
let decodedApiTokens = decodedApiKeytr.split(":");
client.authentications.basicAuth.username = decodedApiTokens[0];
client.authentications.basicAuth.password = decodedApiTokens[1];

var authCallback = function(error, data, response) {
    if (error) {
        console.error("Error: " + JSON.stringify(response));
    } else {
        client.authentications.bearerToken.apiKey = data.accessToken;
        client.authentications.bearerToken.apiKeyPrefix = "Bearer";
        // continue with other API calls.
    }
};
var authApi = new FortanixSdkmsRestApi.AuthenticationApi()
authApi.authorize(authCallback);
```

### REST API using curl

```bash
$ curl <Endpoint URL>/sys/v1/session/auth -X POST -H 'Authorization: Basic <API Key>'
> {"token_type":"Bearer","expires_in":600,"access_token":"YhXwwa- 6C...L9kRxswmPZkEFQ2ig5g","entity_id":"7916b324-33a1-4a06-8778-59ec0492bb10"}

#use the "access_token" as Bearer Auth in other API requests. E.g: 
$ curl <Endpoint URL>/other_apis -H 'Authorization: Bearer YhXwwa- 6C...L9kRxswmPZkEFQ2ig5g ' ...
```

## Related

- [Signature Generation](/dsm-example-code-signature-generation.md)
- [Fortanix DSM with Snowflake for Tokenization](/fortanix-dsm-with-snowflake.md)
- [Fortanix DSM - Quickstart](/fortanix-data-security-manager-quickstart.md)
- [Create a Group](/dsm-example-code-create-a-group.md)
- [Configure API Client](/dsm-example-code-configure-api-client.md)
