Edit

Share via


Quickstart: Install and use a package with the dotnet CLI

In this quickstart, you install the popular Newtonsoft.Json NuGet package into a .NET project. NuGet packages contain compiled binary code that developers make available for other developers to use in their projects. For more information, see An introduction to NuGet.

To install the package, you use the dotnet package add command, which is part of the dotnet command-line interface (CLI).

Tip

Browse nuget.org/packages to find packages you can reuse in your own applications. You can search directly at https://nuget.org/packages, or you can find and install packages from within Visual Studio. For more information, see Find and evaluate NuGet packages for your project.

Prerequisites

The .NET SDK, which provides the dotnet CLI. In Visual Studio, the dotnet CLI automatically installs with any .NET-related workloads.

Create a project

You can install NuGet packages into a .NET project. For this quickstart, take the following steps to create a basic .NET console project by using the dotnet CLI:

  1. Create a folder named Nuget.Quickstart for the project.

  2. Open a command prompt window and go to the new folder.

  3. Create the project by using the following command:

    dotnet new console
    
  4. Use dotnet run to test the app. The command writes the following output to the screen: Hello, World!.

Add the Newtonsoft.Json NuGet package

  1. Use the following command to install the Newtonsoft.Json package:

    dotnet package add Newtonsoft.Json
    

    If you're using .NET 9 or earlier, use the verb-first form instead:

    dotnet add package Newtonsoft.Json
    
  2. After the command finishes, open the Nuget.Quickstart.csproj file in Visual Studio or in a text editor. Check for the added NuGet package reference:

    <ItemGroup>
      <PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
    </ItemGroup>
    

Use the Newtonsoft.Json API in the app

In code, you refer to installed packages by using a using <namespace> directive, where <namespace> is often the package name. You can then use the package's API in your project.

  1. In Visual Studio or in a text editor, open the Program.cs file. Add the following line to the top of the file:

    using Newtonsoft.Json;
    
  2. Add the following code to replace the Console.WriteLine("Hello, World!"); statement:

    namespace Nuget.Quickstart
    {
        public class Account
        {
            public string? Id { get; set; }
            public decimal Balance { get; set; }
            public DateTime Created { get; set; }
        }
        internal class Program
        {
            static void Main(string[] args)
            {
                Account account = new Account
                {
                    Id = "A1bC2dE3fH4iJ5kL6mN7oP8qR9sT0u",
                    Balance = 4389.21m,
                    Created = new DateTime(2026, 4, 16, 0, 0, 0, DateTimeKind.Utc),
                };
    
                string json = JsonConvert.SerializeObject(account, Formatting.Indented);
                Console.WriteLine(json);
            }
        }
    }
    
  3. Save the file, and then build and run the app by using the dotnet run command. The output is the JSON representation of the Account object in the code:

    {
      "Id": "A1bC2dE3fH4iJ5kL6mN7oP8qR9sT0u",
      "Balance": 4389.21,
      "Created": "2026-04-16T00:00:00Z"
    }
    

For videos about using NuGet for package management, see .NET Package Management with NuGet for Beginners and NuGet for Beginners.

To find out more about installing and using NuGet packages by using the dotnet CLI, see the following articles: