# Restoring NuGet Packages in Your CI Workflow

This article provides an overview of the most popular approaches for using token-based authentication to restore Telerik NuGet packages in your CI (Continuous Integration) workflow.

The Telerik NuGet server allows you to authenticate by using two methods:

* Basic authentication by providing your Telerik user name and password.
* Token-based authentication by providing a NuGet Key.

When you need to restore Telerik NuGet packages as part of your CI, using NuGet keys is the more secure way to authenticate. This method does not require you to provide your Telerik username and password anywhere in the CI workflow.

Unlike your Telerik credentials, a NuGet Key has a limited scope and can be used only with the Telerik NuGet server. If any of your NuGet keys is compromised, you can quickly delete it and create a new one.

## Generating NuGet Keys

1. Go to the [Telerik account page for managing NuGet keys](https://www.telerik.com/account/downloads/nuget-keys).

2. Select the **DOWNLOADS** tab and then **Manage NuGet Keys**.

   ![Telerik UI for WPF NuGet keys management page](https://www.telerik.com/products/wpf/documentation/assets/fd1ba3ce09fd69bf2c128cfc930847a0/installation-manage-nuget-keys.png)

3. To create a new key, select the **Generate New Key** button.

4. Enter a name for the NuGet Key, and then select **Generate Key**.

5. To copy the key, select **Copy and Close**. Once you close the window, you can no longer copy the generated key. For security reasons, the **NuGet Keys** page displays only a portion of the key.

   ![Telerik UI for WPF generated NuGet key ready to copy](https://www.telerik.com/products/wpf/documentation/assets/381a915710a0b31db880f52b56159149/installation-copy-nuget-key.png)

## Storing a NuGet Key

> Never check in a NuGet Key with your source code or leave it publicly visible in plain text, for example, as a raw key value in a `nuget.config` file. A NuGet Key is valuable as bad actors can use it to access the NuGet packages that are licensed under your account. A potential key abuse could lead to a review of the affected account.

To protect the NuGet Key, store it as a secret environment variable. The exact steps depend on your workflow:

* In GitHub Actions, save the key as a GitHub Actions Secret. Go to **Settings** > **Security** > **Secrets** > **Actions** > **Add new secret**.

* In Azure DevOps Classic, save the key as a secret pipeline variable. Go to the **Variables** tab and then select **Pipeline variables**.

* In Azure DevOps YAML pipelines, save the key as a secret variable as well. Click the YAML editor's **Variables** button and complete the **New variable** form.

If you use Azure DevOps Service connection instead of secret environment variables, enter `api-key` in the username field and the NuGet Key as the password in the **New NuGet service connection** form editor.

For more details on storing and protecting your NuGet Key, read Lance McCarthy's blog post on [using Telerik NuGet keys for secure package restores](https://www.telerik.com/blogs/announcing-nuget-keys).

## Using a NuGet Key

There are two popular ways to use the Telerik NuGet server in a build:

* [Using a nuget.config file with your projects](https://www.telerik.com/products/wpf/documentation/installation/manual-setup-guides/nuget/nuget-keys#using-a-nuget-config-file-with-your-projects)

* [Using only CLI commands](https://www.telerik.com/products/wpf/documentation/installation/manual-setup-guides/nuget/nuget-keys#using-only-cli-commands)

For more information on how to use NuGet keys in a build, read Lance McCarthy's guide to [configure Telerik NuGet key authentication in build pipelines](https://www.telerik.com/blogs/announcing-nuget-keys).

### Using a nuget.config File with Your Projects

1. In your `nuget.config` file, set the `Username` value to `api-key` and the `ClearTextPassword` value to an environment variable name:

   ```XML
   <configuration>
   	<packageSources>
   		<clear/>
   		<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
   		<add key="MyTelerikFeed" value="https://nuget.telerik.com/v3/index.json" protocolVersion="3"/>
   	</packageSources>
   	<packageSourceCredentials>
   		<MyTelerikFeed>
   			<add key="Username" value="api-key" />
   			<add key="ClearTextPassword" value="%MY_API_KEY%" />
   		</MyTelerikFeed>
   	</packageSourceCredentials>
   	...
   </configuration>
   ```

2. Set the `MY_API_KEY` environment variable by using the value of your pipeline/workflow secret.

The exact steps to set the `MY_API_KEY` environment variable depend on your workflow. For more details, refer to Lance McCarthy's article on [setting up Telerik NuGet keys in CI environments](https://www.telerik.com/blogs/announcing-nuget-keys).

### Using Only CLI Commands

You can use the CLI `add source` (or `update source`) command to set the credentials of a package source. This CLI approach is applicable if your CI system doesn't support default environment variable secrets or if you do not use a custom `nuget.config`.

* To set the credentials in Azure DevOps:

  ```bash
  dotnet nuget add source 'MyTelerikFeed' --source 'https://nuget.telerik.com/v3/index.json' --username 'api-key' --password '$(TELERIK_NUGET_KEY)' --configfile './nuget.config' --store-password-in-clear-text    
  ```

* To set the credentials in GitHub Actions:

  ```bash
  dotnet nuget add source 'MyTelerikFeed' --source 'https://nuget.telerik.com/v3/index.json' --username 'api-key' --password '$' --configfile './nuget.config' --store-password-in-clear-text
  ```

## Additional Resources

If you just start using the Telerik NuGet server in your CI or inter-department workflows, check the two blog posts below. You will learn about the various use cases and find practical implementation details.

* [Use Telerik NuGet Packages in Azure DevOps Pipelines](https://www.telerik.com/blogs/azure-devops-and-telerik-nuget-packages)

* [Understand Telerik NuGet Key Authentication and Usage](https://www.telerik.com/blogs/announcing-nuget-keys)
