FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

C#

This section is intended to help C# developers get started using the Fauna C# driver for application development.

Current version and repository location

Current stable version

0.1.0-beta

Repository

This section provides a high-level overview of working with the driver. For details on the driver’s API, see its documentation.

Installation

Install the Nuget package by adding the package reference to your MSBuild project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="FaunaDB.Client" Version="0.1.0-beta" />
    </ItemGroup>
</Project>

or by using your IDE and searching for FaunaDB.Client.

Example application

The following example code runs a bare-bones C# application which creates a new document in a collection called People.

Prerequisites

  • A Fauna account. If you don’t have one, see the dashboard quick start for help getting set up.

  • A supported version of C# and the Fauna C# driver. See Installation for driver installation help.

Procedure

  1. Navigate to the Fauna Dashboard

    Log in to your Fauna account at Fauna Dashboard if you’re not already logged in.

  2. Create a new database

    Click NEW DATABASE and select your Region Group.

  3. Create a new collection

    Click NEW COLLECTION. Name your new collection People and save it.

  4. Create an access key

    Click SECURITY in the left-side navigation menu. Create a new key for your database. Be sure to save the key’s secret in a safe place, as it is only displayed once.

  5. Create a local environment variable with your access key’s secret

    On MacOS and Linux systems, enter the following in a terminal window:

    export FAUNADB_SECRET=<your-secret>

    For Windows systems, enter the following in a terminal window:

    set FAUNADB_SECRET=<your secret>

    For either example, replace <your secret> with the secret for the access key that you created.

  6. Create a local application

    The following instructions assume you are using Visual Studio Code as a development environment.
  7. Install the .NET SDK

    Download and install .NET version 6.0 or newer.

  8. Create a new project folder

    Create a new folder named myTest in VS Code. This folder holds your project code and supporting files.

  9. Open a terminal window

    Open a new terminal window in VS Code.

  10. Check your .NET version

    To be sure .NET is installed and accessible enter the following command at the terminal prompt:

    dotnet --version

    If .NET is correctly installed on your system, you should see your .NET SDK version number.

  11. Create a new .NET project

    Enter the following command to create a new .NET project:

    dotnet new console

    You should now have some new files in your project folder, including myTest.csproj and Program.cs.

  12. Include the Fauna package with your .csproj file

    Open the myTest.csproj file and add the following lines above the closing </Project> tag:

    <ItemGroup>
      <PackageReference Include="FaunaDB.Client" Version="0.1.0-beta" />
    </ItemGroup>
  13. Edit your Program.cs file

    Remove the placeholder code from the Program.cs file and replace it with the following:

    namespace Example
    {
        using System;
        using System.Threading.Tasks;
        using FaunaDB.Client;
        using FaunaDB.Collections;
        using FaunaDB.Query;
        using FaunaDB.Types;
    
        using static FaunaDB.Query.Language;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                var secret = System.Environment.GetEnvironmentVariable("FAUNADB_SECRET");
                if (secret == null)
                {
                    Console.WriteLine("The FAUNADB_SECRET environment is not set... exiting.");
                    System.Environment.Exit(1);
                }
    
                var endpoint = System.Environment.GetEnvironmentVariable("FAUNADB_ENDPOINT");
                if (endpoint == null)
                {
                    endpoint = "https://db.fauna.com/";
                }
    
                Run(endpoint, secret).Wait();
                System.Environment.Exit(0);
            }
    
            public static async Task Run(string endpoint, string secret)
            {
                var client = new FaunaClient(
                    endpoint: endpoint,
                    secret: secret
                );
    
                try
                {
                    Value result = await client.Query(
                        Create(
                            Collection("People"),
                            Obj(
                                "data", Obj(
                                    "first", "Linus",
                                    "last", "Torvalds",
                                    "age", 52
                                )
                            )
                        )
                    );
                    Console.WriteLine(result);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"ERROR: {e.Message}");
                }
            }
        }
    }

    The above example app uses the Create function to create a new document in the People collection.

  14. Check your environment variable

    At the terminal prompt, enter the following command:

    echo $FAUNADB_SECRET

    You should see the key secret for your Fauna access key. If not, set the environment variable with the following command:

    export FAUNADB_SECRET='your-secret'
  15. Run your program

    At the terminal prompt, enter the following command:

    dotnet run

    You should see results similar to this:

    ObjectV(ref: RefV(id = "280491264236847616", collection = RefV(id = "People", collection = RefV(id = "collections"))),ts: LongV(1603756164680000),data: ObjectV(first: StringV(Linus),last: StringV(Torvalds),age: LongV(52)))

    If you get an error, check to make sure your access key is correct.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!