Go SDK
The Fatture in Cloud Go SDK is a Go library that offers models and methods to interact with the Fatture in Cloud v2 REST API.
If you want to know more generic information about our SDKs, please check the SDK Overview page.
☑️ Requirements and Dependencies
This SDK can be used in Go applications.
⬇️ Download and Installation
The SDK code and detailed documentation can be found in the GitHub repository.
Make sure you always import the newest version of our SDK, you can check which version is the latest on GitHub
The SDK can be installed running the following command:
go get github.com/fattureincloud/fattureincloud-go-sdk
👷 SDK Structure
Our SDK is mainly split into two different packages:
- api: Here you can find the implementation of our API methods, you will need to initialize one of those APIs to start.
- model: This package contains all the representations of our API requests and responses; when using one of the methods above, you'll have to manage some of those models.
There are some models with a special role:
- The models with a name ending for Request can be used as request body for one of our methods.
- The models with a name ending for Response will be returned after the execution of one of the methods. The other models will be used to compose the content of the requests or responses for our methods.
You can think about Request and Response as wrappers: each one of them is dedicated to a single method of the API, and they will most of the time contain a single attribute called data, that contains the real body of the request or the response represented through a composition of the other models. Each method will accept at most one Request model and will return at most one Response model.
Let's take for example the Modify Supplier method.
It is included in the SuppliersApi, it accepts one ModifySupplierRequest and it returns a ModifySupplierResponse. In both cases, the data parameter will contain a Supplier, that represents the modifies to apply to the supplier (for the request) and the final status of the supplier (for the response).
In contrast, the List Suppliers method is still contained in the SuppliersApi, but it doesn't need any request body and returns a single ListSuppliersResponse, where the data parameter will contain an array of Suppliers.
📢 API calls
The API methods can be categorized as follows:
Category (prefix) | Request Body | Response Body | Notes |
---|---|---|---|
List (list) | ❌ | ✅ | 🎩 + 🔃 + 📃 + 🏷 |
Create (create) | ✅ | ✅ | |
Get (get) | ❌ | ✅ | 🎩 |
Modify (modify) | ✅ | ✅ | |
Delete (delete) | ❌ | ❌ |
In addition to the Request, every method could require some additional parameters like the IDs of the company and the resource.
In this example, we'll suppose you have to manage just one Company, so we simply inserted its ID directly in the code. If instead, you need to be able to manage multiple companies, you'll need to retrieve the ID of the current company in some way. Check the Company-scoped Methods page for more info. Additionally, the Go Quickstart contains an example of Company ID retrieval using the SDK.
🎩 Response customization
The List and Post methods include some parameters dedicated to the response customization. These parameters are passed as method arguments.
🔃 Sorting
The List methods are a particular case because they are related to a set of resources instead of a single one; this means that the API will need to assign an order to the resources that will be returned. If needed, you can explicitly define a sorting rule by adding the scope parameter.
📃 Pagination
Strictly related to the Sorting functionality is the Pagination. The List methods return a potentially huge set of resources, making it necessary to paginate the results to make the responses manageable; each method will then accept an additional set of pagination parameters, and the Response classes will contain some pagination details along with the data parameter.
🏷 Filtering
By default, the List methods will return the whole set of available resources for a certain method. If you instead want to focus on a particular subset of resources, you can apply specific filters to reduce the size of the result and retrieve only what you need.
🔑 Authentication & Authorization
This SDK allows you to retrieve and refresh the access token with the integrated OAuth Helper, you can find a complete guide about it here, in case you are using the manual auth you can always set the token manually.
🐤 Getting started
After you followed the installation procedure and retrieve a valid Access Token (see above), you can start using our APIs.
First, you need to import the SDK and initialize the authentication using the token:
package main
import (
"context"
"encoding/json"
"os"
fattureincloudapi "github.com/fattureincloud/fattureincloud-go-sdk/v2/api"
fattureincloud "github.com/fattureincloud/fattureincloud-go-sdk/v2/model"
)
func main() {
// Configure OAuth2 access token for authorization:
auth := context.WithValue(context.Background(), fattureincloudapi.ContextAccessToken, "YOUR_ACCESS_TOKEN")
configuration := fattureincloudapi.NewConfiguration()
}
You can then retrieve one or more of the APIs, for example:
suppliersApi := fattureincloudapi.NewAPIClient(configuration).SuppliersApi
Once you obtained the needed API, you can start using the methods it provides.
If you want to use a method declared in two different APIs, you'll not be able to use the same instance. Instead, you'll need two different instances, one for each of the needed APIs.
Let's implement the ListSuppliers and ModifySupplier methods explained above:
// NOTE: this is a complete request, but please customize it!!!
// In the next step we'll explain how to perform the request to the API.
// in this example we are using our Go SDK
// https://github.com/fattureincloud/fattureincloud-go-sdk
package main
import (
"context"
"encoding/json"
"fmt"
"os"
fattureincloudapi "github.com/fattureincloud/fattureincloud-go-sdk/v2/api"
fattureincloud "github.com/fattureincloud/fattureincloud-go-sdk/v2/model"
)
func main() {
companyId := int32(12345) // int32 | The ID of the company.
fields := "" // string | List of comma-separated fields. (optional)
fieldset := "detailed" // string | Name of the fieldset. (optional)
sort := "-id" // string | List of comma-separated fields for result sorting (minus for desc sorting). (optional)
page := int32(2) // int32 | The page to retrieve. (optional) (default to 1)
perPage := int32(8) // int32 | The size of the page. (optional) (default to 5)
// Configure OAuth2 access token for authorization:
auth := context.WithValue(context.Background(), fattureincloudapi.ContextAccessToken, "YOUR_ACCESS_TOKEN")
configuration := fattureincloudapi.NewConfiguration()
suppliersApi := fattureincloudapi.NewAPIClient(configuration).SuppliersApi
companySuppliers, r, err := suppliersAPI.ListSuppliers(auth, companyId).Fields(fields).Fieldset(fieldset).Sort(sort).Page(page).PerPage(perPage).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `SuppliersAPI.ListSuppliers``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
firstSupplier := companySuppliers.GetData()[0]
modifySupplierRequest := *firstSupplier.SetName("nuovo nome").SetPhone("03561234312")
resp, r, err := suppliersAPI.ModifySupplier(auth, companyId, firstSupplier.GetId()).ModifySupplierRequest(modifySupplierRequest).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `SuppliersAPI.ModifySupplier``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `ModifySupplier`: ModifySupplierResponse
json.NewEncoder(os.Stdout).Encode(resp)
}
You can finally start interacting with the Fatture in Cloud API!
🗒 Retrieve the JSON request body
If you are experiencing some kind of issue and you want to check the raw JSON sent as the body for your request (and maybe send it to our customer support) you can do it as follows:
json.NewEncoder(os.Stdout).Encode(modifySupplierRequest)
🥥 Use raw JSON as request body
If you already have a complete json that you want to use to call our APIs with the SDK without having to build the request object you can do it as follows:
modifySupplierRequest := *fattureincloud.NewModifySupplierRequest()
err := json.Unmarshal([]byte("{\"data\":{\"name\":\"nuovo nome\", \"phone\":\"03561234312\"}}"), &modifySupplierRequest)
❓ What now?
If you need a more detailed example, you can check our Go SDK Quickstart.
Here you can find the full documentation of the available methods and models:
- Go SDK GitHub Repository: the Readme file contains the full list of the available methods and models
- API Reference: it contains the list of methods and models
- OpenAPI Specification: Our OpenAPI Specification contains the full description of the available methods and models