# Trial Licensing

In this guide, we will explore the powerful capabilities of LicenseSpring's trial licensing feature, which enables software vendors to offer time-limited evaluation periods for their products.

### Prerequisites

1. Completed the [**Getting Started**](/sdks/tutorials/getting-started.md) tutorial, specifically:
   * Initialized LicenseManager (or LicenseHandler) with your configuration using the appropriate settings.
   * Created a LicenseID using either LicenseID::fromKey or LicenseID::fromUser function, depending on the activation method you prefer.

### Differences Between Trial and Non-Trial Licenses

Distinguishing trial and non-trial licenses involves several key differences:

1. **Limited Validity:** Trial licenses have a predefined expiration date, restricting the user's access to the software after this date. In contrast, non-trial licenses typically have no time restrictions, allowing perpetual usage.
2. **Identifiability:** The LicenseSpring SDKs can recognize trial licenses as distinct entities. This enables developers to implement conditional logic based on the license type. For instance, specific features can be locked off for trial licenses, providing a tailored trial experience.

### Allowing multiple licenses on trial

Inside Settings -> Preferences -> Allow multiple licenses on trial. This checkbox will allow you to create unlimited trial on same hardware\_id.

![](/files/55077fc8f00112b338f00bbea5369ce188c8add7)

### Getting a Trial License Using the SDK

By integrating a LicenseSpring SDK into your application's codebase, you gain the ability to effortlessly generate and activate trial licenses.

It is shown how to create and enable a key-based trial license below:

{% tabs %}
{% tab title="C++" %}

```cpp
LicenseID LicenseSpring::LicenseManager::getTrialLicense(Customer::ptr_t user = nullptr, const std::string &licensePolicy = std::string())
```

{% endtab %}

{% tab title="C#" %}

```csharp
LicenseID LicenseSpring.LicenseManager.GetTrialLicense(	Customer user = null, string licensePolicy = null )	
```

{% endtab %}

{% tab title="Java" %}

```java
UnactivatedTrialLicense getTrialLicense( com.licensespring.model.Customer customer )
```

{% endtab %}

{% tab title="Swift" %}

```swift
let customer = Customer(...)
let key = try manager.requestTrialKey(with: customer)
```

{% endtab %}

{% tab title="Python" %}

```python
customer = Customer(email='python_policy@gmail.com')  

manager = LicenseManager(conf)

license_id = manager.get_trial_license(customer=customer,license_policy='test')
    
license = manager.activate_license(license_id=license_id)
```

{% endtab %}

{% tab title="Go" %}

```go
// Go SDK v2
	customer := types.Customer{
		Email:     "fa.bagheri.a@gmail.com",
		FirstName: "fatemeh",
		City:      "vancouver",
	}

	ld, err := license_handler.GetTrialKey(customer, "")
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
When using a key-based trial license, we leave the first parameter as a nullptr. When issuing a user-based trial license, we input the user email.
{% endhint %}

{% hint style="info" %}
If we leave the second parameter, or pass it an empty string, it will use our default license policy to create our trial license. Otherwise, we can pass it a specific license policy code to use that policy's license settings.
{% endhint %}

It is also possible to pass a string of an email as the lone parameter, as shown below:

{% tabs %}
{% tab title="C++" %}

```cpp
LicenseID LicenseSpring::LicenseManager::getTrialLicense(const std::string &userEmail)	
```

{% endtab %}

{% tab title="C#" %}

```csharp
LicenseID LicenseSpring.LicenseManager.GetTrialLicense( string 	email )	
```

{% endtab %}

{% tab title="Java" %}

```java
UnactivatedTrialLicense getTrialLicense( String email )
```

{% endtab %}

{% tab title="Swift" %}

```swift
let key = try manager.requestTrialKey(with: "testcustomer@foo.com")
```

{% endtab %}
{% endtabs %}

### Determining if the Current License Is a Trial

It is straightforward to check whether the current license in your software application is a trial license.

Each SDK has a method within the `License` object that allows developers to distinguish between trial and non-trial licenses, as shown below:

{% tabs %}
{% tab title="C++" %}

```cpp
if (license->isTrial())
{
    //Trial license code
}
else
{
    //Normal license code
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
if ( license.IsTrial() )
  {
    //Trial license code
  }
else
  {
    //Normal license code
  }
```

{% endtab %}

{% tab title="Java" %}

```java
if ( license.getData().isTrial() )
  {
    //Trial license code
  }
else
  {
    //Normal license code
  }
```

{% endtab %}

{% tab title="Swift" %}

```swift
license.isTrial
```

{% endtab %}

{% tab title="Python" %}

```python
 if license.is_trial():
     pass #if license is trial
 else:
     pass #license not trial
```

{% endtab %}
{% endtabs %}

### Sample Code for Obtaining and Activating a Trial License

The following sample code illustrates obtaining and activating trial licenses for both key and user-based licenses.

For the sample code to operate for user-based license, simply replace the nullptr in `Customer::ptr_t user = nullptr` with a `Customer` object.

Also, `licensePolicyCode` can be set to the string of any license policy code associated with the license, otherwise if left as empty it will use the default license policy.

{% tabs %}
{% tab title="C++" %}

```cpp
Customer::ptr_t user = nullptr;
const std::string licensePolicyCode = std::string();
LicenseID licenseId;
    
try
{
    licenseId = licenseManager->getTrialLicense(user, license_policy_code);
    // licenseId = licenseManager->getTrialLicense(user->email);
}
catch (TrialNotAllowedException)
{
    //Trial is not allowed on current product + license_policy combination
}
catch (ProductNotFoundException)
{
    //Product could not be found on server. Please make sure your API key, Shared key, and Product Code are all correct.
}
catch (MissingEmailException)
{
    //User based products require an email, even for trial licenses.
 }
catch (...)
 {
    //Possible network issue.
 }
```

{% endtab %}

{% tab title="Python" %}

```python
manager= LicenseManager(conf)

customer = Customer(email='python_policy@gmail.com')  

manager = LicenseManager(conf)

license_id = manager.get_trial_license(customer=customer,license_policy='test')

try:
    license = manager.activate_license(license_id=license_id)

except Exception as ex:
    print(ex)
```

{% endtab %}

{% tab title="Java" %}

```java
UnactivatedTrialLicense trial = licenseManager.getTrialLicense("python_policy@gmail.com");
License license = licenseManager.activateLicense(trial.createIdentity());
```

{% endtab %}
{% endtabs %}

After activating a trial license, developers can control the state of the application through the `isTrial()` method accessible through the `License` object.

This functionality is shown below:

{% tabs %}
{% tab title="C++" %}

```cpp
// Creating or updating trial license.
license = licenseManager->activateLicense(licenseId);

if (license->isTrial())
{
    //Currently using a trial license.
    //Showing limited options.
}
else
{
    //Currently using a non-trial license.
    //Showing full account options.
 }
```

{% endtab %}

{% tab title="Python" %}

```python
manager= LicenseManager(conf)

customer = Customer(email='python_policy@gmail.com')  

manager = LicenseManager(conf)

license_id = manager.get_trial_license(customer=customer,license_policy='test')
    
license = manager.activate_license(license_id=license_id)
        
if license.is_trial():
    pass #limited options

else:
    pass #full set of features
```

{% endtab %}

{% tab title="Java" %}

```java
UnactivatedTrialLicense trial = licenseManager.getTrialLicense("python_policy@gmail.com");
License license = licenseManager.activateLicense(trial.createIdentity());
LicenseData data = license.getData();

if (data.isTrial()) {
  // limited options
} else {
  // full set of features
}
```

{% endtab %}
{% endtabs %}

### Troubleshooting

#### Exceptions Related to Trial Licensing

Exceptions that might occur when performing offline license activation:

* **TrialNotAllowedException:** Trial is not allowed by product license policy.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.licensespring.com/sdks/tutorials/licensing-scenarios/trial-licensing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
