SDKs overview
Micro User Service provides Software Development Kits (SDKs) and libraries designed to streamline the integration of user management functionalities into applications built on the Micro platform. These tools enable developers to interact with the User Service API for tasks such as creating and managing user accounts, handling authentication, and enforcing authorization policies within a distributed system environment. The official SDKs are available for Go and JavaScript, supporting the primary languages used within the Micro ecosystem.
Using SDKs typically abstracts away the complexities of direct API calls, including HTTP request handling, data serialization (e.g., JSON), and error management. This abstraction allows developers to focus on application logic rather than low-level communication protocols. SDKs are an integral part of enhancing the developer experience with Micro User Service, as they ensure consistency and reduce the boilerplate code required for common operations. The Micro platform's broader ecosystem, including the Auth Service and API Gateway, benefits from these SDKs by providing a cohesive development experience for building microservices.
Official SDKs by language
Micro User Service officially supports SDKs for Go and JavaScript, which are the primary languages favored for development within the Micro platform. These SDKs are maintained by the Micro team and are designed to provide stable, idiomatic interfaces for interacting with the User Service API. They cover core functionalities such as user registration, login, session management, and profile updates.
The table below summarizes the key details for the official SDKs:
| Language | Package/Module | Installation Command | Maturity |
|---|---|---|---|
| Go | go-micro/v4/client/user |
go get go-micro.dev/v4/client/user |
Stable |
| JavaScript | @micro/user-client |
npm install @micro/user-client or yarn add @micro/user-client |
Stable |
These SDKs are designed to integrate seamlessly with the Micro runtime and services, ensuring compatibility and optimal performance within a microservices architecture. They are regularly updated to reflect changes and improvements in the Micro User Service API.
Installation
Installing the Micro User Service SDKs involves using the respective language's package manager. The following instructions detail the process for Go and JavaScript.
Go SDK Installation
To install the Go SDK for Micro User Service, you typically use the go get command. This command fetches the module and its dependencies, making it available for use in your Go projects. Ensure you have a working Go environment set up.
go get go-micro.dev/v4/client/user
After installation, you can import the package into your Go application:
import (
"context"
"log"
microclient "go-micro.dev/v4/client"
userservice "go-micro.dev/v4/client/user"
)
For more detailed information on Go module management, refer to the official Go modules documentation.
JavaScript SDK Installation
For JavaScript projects, you can install the Micro User Service client library using either npm or yarn. These package managers handle the installation of the module and its dependencies.
Using npm:
npm install @micro/user-client
Using yarn:
yarn add @micro/user-client
Once installed, you can import the library into your JavaScript or TypeScript application:
import { UserService } from '@micro/user-client';
// Or for CommonJS:
// const { UserService } = require('@micro/user-client');
For further guidance on managing JavaScript dependencies, consult the npm documentation or Yarn documentation.
Quickstart example
The following quickstart examples demonstrate basic usage of the Micro User Service SDKs for common tasks like user registration and login. These examples assume that the Micro User Service is running and accessible.
Go Quickstart: Registering a user
This Go example shows how to use the User Service client to register a new user.
package main
import (
"context"
"log"
microclient "go-micro.dev/v4/client"
userservice "go-micro.dev/v4/client/user"
"go-micro.dev/v4"
)
func main() {
// Create a new service
service := micro.NewService(
micro.Name("user-registration-client"),
)
service.Init()
// Create a new user service client
userService := userservice.NewUserService("user", service.Client())
// Register a new user
rsp, err := userService.Register(
context.Background(),
&userservice.RegisterRequest{
Email: "[email protected]",
Password: "securepassword",
Username: "testuser",
},
)
if err != nil {
log.Fatalf("Failed to register user: %v", err)
}
log.Printf("User registered successfully. User ID: %s", rsp.User.Id)
}
This code snippet initializes a Micro service client, then uses it to call the Register method on the User Service. It logs the user ID upon successful registration or an error if the operation fails. More examples can be found in the Micro User Service API reference.
JavaScript Quickstart: Logging in a user
This JavaScript example demonstrates how to use the @micro/user-client to log in an existing user and retrieve a session token.
import { UserService } from '@micro/user-client';
const main = async () => {
// Initialize the user service client
// In a real application, you might pass a client configuration
const userService = new UserService();
try {
// Log in the user
const response = await userService.login({
email: '[email protected]',
password: 'securepassword',
});
console.log('User logged in successfully.');
console.log('Session Token:', response.session?.token);
} catch (error) {
console.error('Failed to log in user:', error);
}
};
main();
This JavaScript code initializes the UserService, then attempts to call the login method with user credentials. It prints the session token if the login is successful or catches and logs any errors that occur. For further JavaScript examples and API details, consult the Micro User Service documentation.
Community libraries
While Micro User Service provides official SDKs for Go and JavaScript, the open-source nature of the Micro platform encourages community contributions. Developers can create and share libraries, wrappers, and integrations that extend the functionality or support additional programming languages not officially covered. These community-driven efforts can range from alternative client implementations to higher-level abstractions built on top of the official SDKs.
Community libraries often emerge in ecosystems where developers need to integrate Micro User Service with specific frameworks, tools, or languages like Python, Ruby, or Java. These libraries might also provide domain-specific functionality or simplify common patterns not directly addressed by the official SDKs.
When considering community libraries, it is important to evaluate their maintenance status, documentation, and the level of community support. Developers should refer to the Micro GitHub organization and related community forums for discovering and assessing third-party contributions. The quality and reliability of community libraries can vary, so due diligence is recommended before incorporating them into production systems.
Examples of potential community contributions could include:
- Python client library: A wrapper allowing Python applications to interact with Micro User Service.
- Java client library: Facilitating integration into Java-based microservices.
- Framework-specific integrations: Libraries that provide idiomatic integrations for popular web frameworks like React, Vue, or Angular on the frontend, or specific backend frameworks that manage authentication flows using Micro User Service.
- CLI extensions: Tools that extend the existing Micro CLI with additional user management commands.
These community efforts complement the official SDKs by broadening the accessibility and utility of Micro User Service across diverse development environments.