The Crowdin Go client is a lightweight interface to the Crowdin API. It provides common services for making API requests.
Our API is a full-featured RESTful API that helps you to integrate localization into your development process. The endpoints that we use allow you to easily make calls to retrieve information and to execute actions needed.
go get github.com/crowdin/crowdin-api-client-go
Create a new Crowdin client, then use the exposed services to access different parts of the Crowdin API.
You can generate Personal Access Token in your Crowdin Account Settings.
import "github.com/crowdin/crowdin-api-client-go/crowdin"
client, err := crowdin.NewClient(
os.Getenv("CROWDIN_ACCESS_TOKEN"),
crowdin.WithOrganization("organization-name"), // optional for Crowdin Enterprise
)
For example, to create a new project:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/crowdin/crowdin-api-client-go/crowdin"
"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)
func main() {
client, err := crowdin.NewClient(os.Getenv("CROWDIN_ACCESS_TOKEN"))
if err != nil {
log.Fatalf("Error creating client: %s", err)
}
ctx := context.Background()
request := &model.ProjectsAddRequest{
Name: "My Project",
SourceLanguageID: "en",
TargetLanguageIDs: []string{"uk", "de"},
}
project, _, err := client.Projects.Add(ctx, request)
if err != nil {
log.Fatalf("Error creating project: %s", err)
}
fmt.Printf("Project: %+v\n", project)
}
Some API methods have optional parameters that can be passed.
For example, to list all projects for a specific user:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/crowdin/crowdin-api-client-go/crowdin"
"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)
func main() {
client, err := crowdin.NewClient(os.Getenv("CROWDIN_ACCESS_TOKEN"))
if err != nil {
log.Fatalf("Error creating client: %s", err)
}
// list all projects for a specific user
opts := &model.ProjectsListOptions{UserID: 1}
projects, _, err := client.Projects.List(context.Background(), opts)
if err != nil {
log.Fatalf("Error getting projects: %s", err)
}
fmt.Printf("Projects: %+v\n", projects)
}
In case of an error, the client returns an error object. This can either be a generic error with an error message and a code, or a validation error that additionally contains validation error codes.
To detect this condition of error, you can use a type assertion:
res, _, err := client.SourceStrings.Add(ctx, 1, nil)
if err != nil {
if validationErr, ok := err.(*model.ValidationErrorResponse); ok {
fmt.Printf("Validation error: %v\n", validationErr)
} else
fmt.Printf("Error: %v\n", err)
}
}
To set a timeout for HTTP requests, you can pass a custom HTTP client with a timeout to the client.
You can also use the context package, you can pass cancellation signals and deadlines to various services of the client to handle a request.
client, err := crowdin.NewClient(
os.Getenv("CROWDIN_ACCESS_TOKEN"),
crowdin.WithHTTPClient(&http.Client{Timeout: 10 * time.Second}),
)
This library also provides possibility to use GraphQL API.
package main
import (
"context"
"fmt"
"log"
"github.com/crowdin/crowdin-api-client-go/crowdin"
)
func main() {
client, _ := crowdin.NewClient(token)
// Create a new GraphQL query request.
query := client.GraphQL.NewRequest(`
query Demo( $projectLimit: Int!, $withTranslations: Boolean! ) {
viewer {
projects( first: $projectLimit ) {
edges {
node {
id
name
description
translations(first: 20, languageId: "en") @include(if: $withTranslations) {
edges {
node {
... on PlainStringTranslation {
text
}
}
}
}
}
}
totalCount
}
}
}
`)
// Set the variables to be used in the query.
query.Var("projectLimit", 2)
query.Var("withTranslations", true)
// Capture the response in a map.
// Can also be a struct with the same fields as the response.
var resp map[string]any
// Send the query to the server.
if err := client.GraphQL.Query(context.Background(), query, &resp); err != nil {
log.Fatalf("graphql: %v", err)
}
fmt.Printf("%+v\n", resp)
}
If you find any problems or would like to suggest a feature, please read the How can I contribute section in our contributing guidelines.
If you want to contribute please read the Contributing guidelines.
The Crowdin Go client is licensed under the MIT License. See the LICENSE file distributed with this work for additional information regarding copyright ownership. Except as contained in the LICENSE file, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.