Client application quick start

Fauna supports drivers in several popular programming languages for programmatic management and manipulation of Fauna databases. To get started, complete the prerequisites and then select a language.

For a complete list of supported drivers, see Drivers.

Prerequisites

To use a Fauna driver, you must have:

  1. Sign up for a free account

  2. Create a database

    Click NEW DATABASE. Select the Classic region group.

  3. 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.

For a more detailed description of the signup and database creation steps, see the Dashboard quick start.

When you’re ready with a database and an access key, select a language to get started with a driver.

Choose a language

C#GoJavaJavaScriptPython

 

JavaScript

For complete JavaScript driver information, see the driver page.

  1. Install the driver

    Enter the following at the command line:

    terminalCopied!
    npm install --save faunadb
  2. Create a program file

    With your preferred text editor, create a new file called app.js. We’ll use this file to run a Fauna query which creates a new collection named myCollection.

    Add the following lines to your file:

    Copied!
    // Require the driver
    const faunadb = require('faunadb')
    const q = faunadb.query
    
    // Acquire the secret and optional endpoint from environment variables
    const secret = process.env.FAUNADB_SECRET
    var endpoint = process.env.FAUNADB_ENDPOINT
    
    if (typeof secret === 'undefined' || secret === '') {
      console.error('The FAUNADB_SECRET environment variable is not set, exiting.')
      process.exit(1)
    }
    
    if (!endpoint) endpoint = 'https://db.fauna.com/'
    
    var mg, domain, port, scheme
    if ((mg = endpoint.match(/^(https?):\/\/([^:]+)(:(\d+))?/))) {
      scheme = mg[1] || 'https'
      domain = mg[2] || 'db.fauna.com'
      port = mg[4] || 443
    }
    
    // Instantiate a client
    const client = new faunadb.Client({
      secret: secret,
      domain: domain,
      port: port,
      scheme: scheme,
    })
    
    // Create a collection called 'myCollection'
    client.query(
      q.({ name: 'myCollection' })
    )
    
    // Show the result
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
  3. Run the program

    Run the following command in a terminal window, in the directory where you created the program file. Be sure to replace YOUR_FAUNA_SECRET with the secret that you created at the beginning of this guide.

    terminalCopied!
    FAUNADB_SECRET=YOUR_FAUNA_SECRET node app.js

    You should see the result of your CreateCollection query:

    {
      ref: ("myCollection"),
      ts: 1633458045000000,
      history_days: 30,
      name: 'myCollection'
    }
  4. Learn more

    Fauna’s query model relies on indexes to support all query patterns which do not involve looking up documents directly by their Reference. An understanding of index creation and usage is crucial for effective Fauna development.

    See the Index tutorials for more information.

    To learn more about Fauna, Fauna Query Language, and the JavaScript driver, explore the documentation:

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!