# NinjaTrader 8

## **Adding licensing to your NinjaScript**

After you have created your NinjaScript, you will need to integrate [Licensing Library](https://jiribeloch.gitbook.io/poshtrader-help-center/developer/licensing-library) to validate users' licenses and protect your product from unauthorized distribution.

1. Save [**PoshTrader.Licensing.dll**](https://jiribeloch.gitbook.io/poshtrader-help-center/developer/licensing-library/..#shared-licensing-library) into **%UserProfile%\Documents\NinjaTrader 8\bin\Custom\\**
2. Restart the NinjaTrader platform so it can load the external library
3. Add the library as a reference (right-click in **NinjaScript Editor > References…**)
4. Import namespace of the library by adding `using PoshTrader.Licensing;`
5. Add **Item Attributes** to the main class
6. Validate library origin to prevent tampering
7. Create `ItemLicense` on the startup
8. Check license status during runtime

{% tabs %}
{% tab title="Indicator" %}

```csharp
using System;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using PoshTrader.Licensing;

namespace NinjaTrader.NinjaScript.Indicators
{
    [Item(ID, "Sample Indicator", Version = "1.0.0", Author = "John Doe")]
    public class SampleIndicator : Indicator
    {
        private ItemLicense _license;

        protected override void OnStateChange()
        {
            if (State == State.Configure)
            {
                try
                {
                    var assembly = Assembly.GetAssembly(typeof(ItemLicense));
                    var certificate = X509Certificate.CreateFromSignedFile(assembly.Location);
                    if (certificate.GetCertHashString() != "07FF85A977EF497368AA1ADEB64D43B2B55BD4D8")
                    {
                        throw new Exception("Could not validate 'PoshTrader.Licensing' assembly origin.");
                    }
                }
                catch (CryptographicException)
                {
                    throw new Exception("Could not load 'PoshTrader.Licensing' assembly certificate. Platform restart might be required.");
                }

            }
            else if (State == State.DataLoaded)
            {
                _license = new Licensing(this);

                if (!_license.IsValid())
                {
                    throw new Exception("Product license validation failed.");
                }
            }
        }

        protected override void OnBarUpdate()
        {
            if (!_license.IsValid())
                return;

            // Put your OnBarUpdate logic here
        }
    }
}
```

{% endtab %}
{% endtabs %}

## Creating a distribution file

To create the distribution file, please follow the steps below for creating an archive file containing your NinjaScript.

1. From the **Control Center** window select the menu **Tools > Export > NinjaScript Add-On…** to open the **“Export NinjaScript”** dialog window
2. Select the option **“Export as compiled assembly”**
3. Select **“Protect compiled assembly”** (For information on protection [read here](https://ninjatrader.com/support/helpGuides/nt8/protection_dll_security.htm))
4. Press **`Add`** button
5. Select all of the files required, including `References – PoshTrader.Licensing` and press **`OK`**
6. A list of all files that will be exported will be shown
7. Press the **`Export`** button to export the selected files
8. A file dialog will open where you can choose the location to create your distribution file.
9. Add **`PoshTrader.Licensing.dll`** to the exported archive file.

{% hint style="info" %}
By default, the NinjaScript Archive File (**.zip**) file will be created in **%UserProfile%\Documents\NinjaTrader 8\bin\Custom\ExportNinjaScript**
{% endhint %}
