| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The Glean Go SDK provides convenient access to the Glean REST API for Go 1.18+. It offers strongly typed request and response structs, context-based request handling, and uses the standard net/http package.
This SDK combines both the Client and Indexing API namespaces into a single unified package:
Each namespace has its own authentication requirements and access patterns. While they serve different purposes, having them in a single SDK provides a consistent developer experience across all Glean API interactions.
// Example of accessing Client namespace
s := apiclientgo.New(
apiclientgo.WithSecurity("client-token"),
)
res, err := s.Client.Search.Query(ctx, components.SearchRequest{
Query: "search term",
})
// Example of accessing Indexing namespace
s := apiclientgo.New(
apiclientgo.WithSecurity("indexing-token"),
)
res, err := s.Indexing.Documents.Index(ctx, components.DocumentRequest{
// document data
})Remember that each namespace requires its own authentication token type as described in the Authentication Methods section.
To add the SDK as a dependency to your project:
go get github.com/gleanwork/api-client-gopackage main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Client.Chat.Create(ctx, components.ChatRequest{
Messages: []components.ChatMessage{
components.ChatMessage{
Fragments: []components.ChatMessageFragment{
components.ChatMessageFragment{
Text: apiclientgo.Pointer("What are the company holidays this year?"),
},
},
},
},
}, nil, nil)
if err != nil {
log.Fatal(err)
}
if res.ChatResponse != nil {
// handle response
}
}package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Client.Chat.CreateStream(ctx, components.ChatRequest{
Messages: []components.ChatMessage{
components.ChatMessage{
Fragments: []components.ChatMessageFragment{
components.ChatMessageFragment{
Text: apiclientgo.Pointer("What are the company holidays this year?"),
},
},
},
},
}, nil)
if err != nil {
log.Fatal(err)
}
if res.ChatRequestStream != nil {
// handle response
}
}This SDK supports the following security scheme globally:
| Name | Type | Scheme | Environment Variable |
|---|---|---|---|
| APIToken | http | HTTP Bearer | GLEAN_API_TOKEN |
You can configure it using the WithSecurity option when initializing the SDK client instance. For example:
package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Agents.Search(ctx, components.PlatformAgentsSearchRequest{
Name: apiclientgo.Pointer("HR Policy Agent"),
})
if err != nil {
log.Fatal(err)
}
if res.PlatformAgentsSearchResponse != nil {
// handle response
}
}Glean supports different authentication methods depending on which API namespace you're using:
The Client namespace supports two authentication methods:
Manually Provisioned API Tokens
OAuth
The Indexing namespace supports only one authentication method:
Important
Client tokens will not work for Indexing operations, and Indexing tokens will not work for Client operations. You must use the appropriate token type for the namespace you're accessing.
For more information on obtaining the appropriate token type, please contact your Glean administrator.
Available methodsAddOrUpdate - Index document
Index - Index documents
BulkIndex - Bulk index documents
ProcessAll - Schedules the processing of uploaded documents
Delete - Delete document
Debug - Beta: Get document information
DebugMany - Beta: Get information of a batch of documents
CheckAccess - Check document access
Status - Get document upload and indexing status ⚠️ Deprecated
Count - Get document count ⚠️ Deprecated
DebugEvents - Beta: Get document lifecycle events
Debug - Beta: Get user information
Count - Get user count ⚠️ Deprecated
Index - Index employee
BulkIndex - Bulk index employees ⚠️ Deprecated
ProcessAllEmployeesAndTeams - Schedules the processing of uploaded employees and teams
Delete - Delete employee
IndexTeam - Index team
DeleteTeam - Delete team
BulkIndexTeams - Bulk index teams
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:
package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"github.com/gleanwork/api-client-go/retry"
"log"
"models/operations"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Agents.Search(ctx, components.PlatformAgentsSearchRequest{
Name: apiclientgo.Pointer("HR Policy Agent"),
}, operations.WithRetries(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}))
if err != nil {
log.Fatal(err)
}
if res.PlatformAgentsSearchResponse != nil {
// handle response
}
}If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:
package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"github.com/gleanwork/api-client-go/retry"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithRetryConfig(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}),
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Agents.Search(ctx, components.PlatformAgentsSearchRequest{
Name: apiclientgo.Pointer("HR Policy Agent"),
})
if err != nil {
log.Fatal(err)
}
if res.PlatformAgentsSearchResponse != nil {
// handle response
}
}Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.
By Default, an API error will return apierrors.APIError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.
For example, the Search function may return the following errors:
| Error Type | Status Code | Content Type |
|---|---|---|
| apierrors.PlatformProblemDetailError | 400, 401, 403, 404, 408, 413, 429 | application/problem+json |
| apierrors.PlatformProblemDetailError | 500, 503 | application/problem+json |
| apierrors.APIError | 4XX, 5XX | */* |
package main
import (
"context"
"errors"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/apierrors"
"github.com/gleanwork/api-client-go/models/components"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Agents.Search(ctx, components.PlatformAgentsSearchRequest{
Name: apiclientgo.Pointer("HR Policy Agent"),
})
if err != nil {
var e *apierrors.PlatformProblemDetailError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *apierrors.PlatformProblemDetailError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *apierrors.APIError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
}
}The default server https://{instance}-be.glean.com contains variables and is set to https://instance-name-be.glean.com by default. To override default values, the following options are available when initializing the SDK client instance:
| Variable | Option | Default | Description |
|---|---|---|---|
| instance | WithInstance(instance string) | "instance-name" | The instance name (typically the email domain without the TLD) that determines the deployment backend. |
package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithServerIndex(0),
apiclientgo.WithInstance("instance-name"),
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Agents.Search(ctx, components.PlatformAgentsSearchRequest{
Name: apiclientgo.Pointer("HR Policy Agent"),
})
if err != nil {
log.Fatal(err)
}
if res.PlatformAgentsSearchResponse != nil {
// handle response
}
}The default server can be overridden globally using the WithServerURL(serverURL string) option when initializing the SDK client instance. For example:
package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithServerURL("https://instance-name-be.glean.com"),
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Agents.Search(ctx, components.PlatformAgentsSearchRequest{
Name: apiclientgo.Pointer("HR Policy Agent"),
})
if err != nil {
log.Fatal(err)
}
if res.PlatformAgentsSearchResponse != nil {
// handle response
}
}The server URL can also be overridden on a per-operation basis, provided a server list was specified for the operation. For example:
package main
import (
"context"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/operations"
"log"
"os"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
)
res, err := s.Indexing.Datasources.Submit(ctx, "<value>", "<value>", map[string]any{
"key": "<value>",
"key1": "<value>",
"key2": "<value>",
}, operations.WithServerURL("https://instance-name-be.glean.com"))
if err != nil {
log.Fatal(err)
}
if res.Object != nil {
// handle response
}
}The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.
import (
"net/http"
"time"
"github.com/gleanwork/api-client-go"
)
var (
httpClient = &http.Client{Timeout: 30 * time.Second}
sdkClient = apiclientgo.New(apiclientgo.WithClient(httpClient))
)This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.
This SDK defines the following custom types to assist with marshalling and unmarshalling data.
types.Date is a wrapper around time.Time that allows for JSON marshaling a date string formatted as "2006-01-02".
d1 := types.NewDate(time.Now()) // returns *types.Date
d2 := types.DateFromTime(time.Now()) // returns types.Date
d3, err := types.NewDateFromString("2019-01-01") // returns *types.Date, error
d4, err := types.DateFromString("2019-01-01") // returns types.Date, error
d5 := types.MustNewDateFromString("2019-01-01") // returns *types.Date and panics on error
d6 := types.MustDateFromString("2019-01-01") // returns types.Date and panics on errorThe SDK provides options to test upcoming API changes before they become the default behavior. This is useful for:
You can configure these options either via environment variables or SDK constructor options:
export X_GLEAN_EXCLUDE_DEPRECATED_AFTER="2026-10-15"
export X_GLEAN_INCLUDE_EXPERIMENTAL="true"package main
import (
"context"
"log"
"os"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
apiclientgo.WithServerURL("https://mycompany-be.glean.com"),
)
res, err := s.Client.Search.Query(ctx, components.SearchRequest{
Query: "test",
}, nil)
if err != nil {
log.Fatal(err)
}
// Headers are automatically set based on environment variables
log.Println(res)
}package main
import (
"context"
"log"
"os"
apiclientgo "github.com/gleanwork/api-client-go"
"github.com/gleanwork/api-client-go/models/components"
)
func main() {
ctx := context.Background()
s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
apiclientgo.WithServerURL("https://mycompany-be.glean.com"),
apiclientgo.WithExcludeDeprecatedAfter("2026-10-15"),
apiclientgo.WithIncludeExperimental(true),
)
res, err := s.Client.Search.Query(ctx, components.SearchRequest{
Query: "test",
}, nil)
if err != nil {
log.Fatal(err)
}
log.Println(res)
}| Option | Environment Variable | Type | Description |
|---|---|---|---|
| WithExcludeDeprecatedAfter | X_GLEAN_EXCLUDE_DEPRECATED_AFTER | string (date) | Exclude API endpoints that will be deprecated after this date (format: YYYY-MM-DD). Use this to test your integration against upcoming deprecations. |
| WithIncludeExperimental | X_GLEAN_INCLUDE_EXPERIMENTAL | bool | When true, enables experimental API features that are not yet generally available. Use this to preview and test new functionality. |
Note: Environment variables take precedence over SDK constructor options when both are set.
Warning: Experimental features may change or be removed without notice. Do not rely on experimental features in production environments.
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
| Back | FazBrowse Home | New Git URL |