Go Client Library

On this page

The Go client library integrates FusionAuth with your Go application. The source code is in the Go client GitHub repository.

Install#

The Go client library requires Go 1.23 or later.

Create a directory for your project, and initialize a Go module in the directory:

$ mkdir fusionauth
$ cd fusionauth
$ go mod init example.com/fusionauth

Add the Go client library to the module:

$ go get github.com/FusionAuth/go-client/pkg/fusionauth

Retrieve a User#

Put the following code in a file named fusionauth.go in the project directory. The code retrieves a User by email address. Replace YOUR_API_KEY with an API key from Settings -> API Keys in the Admin UI.

package main

import (
    "net/http"
    "net/url"
    "time"
    "fmt"

    "github.com/FusionAuth/go-client/pkg/fusionauth"
)

const host = "http://localhost:9011"

var apiKey = "YOUR_API_KEY"

var httpClient = &http.Client{
	Timeout: time.Second * 10,
}

var baseURL, _ = url.Parse(host)

// Construct a new FusionAuth client
var client = fusionauth.NewClient(httpClient, baseURL, apiKey)

func main() {
    response, errors, err := client.RetrieveUserByEmail("user@example.com")
    if err != nil {
        // transport layer error, such as a failed connection
        fmt.Println(err)   
        return
    }
    if errors != nil {
        // FusionAuth response error, such as a missing user
        fmt.Println(response.StatusCode)
        return
    }
    fmt.Println(response.User.Email)   
    fmt.Println(response.User.FirstName)   
    fmt.Println(response.User.LastName)   
}

Build an executable:

$ go build

Run the executable:

$ ./fusionauth

Usage Suggestions#

Choose a Use Case#

Client libraries wrap the REST API. You can use a client library for two tasks.

The main task is calling the FusionAuth APIs from code. You get auto-completion and typed objects, which helps when you script configuration, automate common tasks, or copy existing Applications and Groups. The API documentation lists the JSON structure of every request and which fields each call requires. When you need the details, read the API documentation and the library source code.

The secondary task is exchanging an authorization code for tokens in the Authorization Code grant. For this task, use an OAuth library for your language instead. For a list of libraries, see the community curated list.

Client libraries don't provide higher level functionality such as token management. To request that functionality, comment on or upvote the open issue.

When a client library doesn't fit your needs, call the REST API directly.

Requests and Responses#

Each library maps a request object to the JSON that the matching API method requires. Depending on the API call, the request object is a set of string parameters or a complex object. When you use a language without static typing, read the API documentation for the operation to learn the expected fields.

Each library returns a response object with these fields:

  • A status that matches the HTTP status code from the API. The status is -1 when no HTTP request succeeded.
  • A JSON success object when the call succeeds.
  • A JSON error object with a message when the status is 4xx or 5xx.
  • An exception object when the library sent no request or the server returned no usable response.

Use Separate Clients for API Key Scopes#

When a script calls endpoints with different API key scopes, create one client instance per scope. For example, Entities accept a tenant-scoped API key, but Entity Types and Lambdas require a global API key.

Follow these rules when your library lets you set a Tenant Id on the client instance:

  • Set the Tenant Id on the tenant-scoped client only.
  • Don't change the Tenant Id on a shared client between calls.
  • Don't send a tenant header to a global-only endpoint. The endpoint fails with a 401.

PATCH Requests#

Available since 1.14.0

PATCH operations modify only parts of an object in FusionAuth. The Go client library is statically typed, so the library doesn't use strongly typed objects for a PATCH request. Instead, you pass a hash, dictionary, or map of key value pairs. Build a multi-level dictionary that creates JSON with nested keys. Otherwise the PATCH request fails.

For example, to change only the name of an Application with PATCH, send this JSON:

Example PATCH Application JSON
{
  "application": {
     "name": "hooli-bought-us"
   }
}

A typed request object serializes with empty arrays and other default values. Those values change the object in ways you don't expect. Nested key value pairs avoid the problem.

This limitation comes from the current implementation of the Go client library and FusionAuth PATCH support. To support typed objects, the domain objects would need no default values, and the serializer would need to omit empty objects, empty arrays, and other empty values.

To use typed objects today, retrieve the object, modify the object in memory, and then update the object. This sequence works the same as a single PATCH operation.

For more information about future support, see the open issue to handle PATCH requests with more nuance. The issue may add options for configuring a JSON serializer to allow typed domain objects with PATCH.

Example Apps#

Update Policy#

FusionAuth releases new versions of the SDKs and client libraries to track the FusionAuth API. SDKs and client libraries may also receive bug fixes, security patches, tests, code samples, and documentation changes.

These releases may update dependencies, language engines, and operating systems, because FusionAuth follows the deprecation and sunsetting policies of the underlying technologies. When the maintainer deprecates a language, framework, or operating system, FusionAuth deprecates the SDKs and client libraries that depend on the technology. FusionAuth then updates the SDKs and client libraries to use a newer version.

For more information, see Update a Client Library.