Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 1.11 KB

doc_4_authentication.md

File metadata and controls

32 lines (24 loc) · 1.11 KB

Authenticate using the Atlas Go SDK

The atlas-sdk-go library uses Digest authentication. You can create an API key through the Atlas UI or the Atlas CLI.

To learn more about API authentication, see Atlas Administration API Authentication.

Use the Atlas Go SDK in Your Code

Construct a new Atlas SDK client, then use the services on the client to access different parts of the Atlas Admin API. For example:

import "go.mongodb.org/atlas-sdk/v20231115008/admin"

func example() {
	ctx := context.Background()

	apiKey := os.Getenv("MONGODB_ATLAS_PUBLIC_KEY")
	apiSecret := os.Getenv("MONGODB_ATLAS_PRIVATE_KEY")

	sdk, err := admin.NewClient(admin.UseDigestAuth(apiKey, apiSecret))
	if err != nil {
		log.Fatalf("Error when instantiating new client: %v", err)
	}
	projects, response, err := sdk.ProjectsApi.ListProjects(ctx).Execute()
	if err != nil {
		log.Fatalf("Could not fetch projects: %v", err)
	}
	fmt.Printf("Response status: %v\n", response.Status)
}