Documentation
¶
Overview ¶
Package fipe is a client for the FIPE API (https://fipe.api.br), which provides average vehicle prices from Brazil's Tabela FIPE.
Index ¶
- Constants
- Variables
- type APIError
- type Brand
- type Client
- func (c *Client) Brands(ctx context.Context, vt VehicleType, opts ...RequestOption) ([]Brand, error)
- func (c *Client) HistoryByFipeCode(ctx context.Context, vt VehicleType, fipeCode, yearID string, ...) (*Vehicle, error)
- func (c *Client) Models(ctx context.Context, vt VehicleType, brandID string, opts ...RequestOption) ([]Model, error)
- func (c *Client) ModelsByBrandYear(ctx context.Context, vt VehicleType, brandID, yearID string, ...) ([]Model, error)
- func (c *Client) References(ctx context.Context) ([]Reference, error)
- func (c *Client) Vehicle(ctx context.Context, vt VehicleType, brandID, modelID, yearID string, ...) (*Vehicle, error)
- func (c *Client) VehicleByFipeCode(ctx context.Context, vt VehicleType, fipeCode, yearID string, ...) (*Vehicle, error)
- func (c *Client) Years(ctx context.Context, vt VehicleType, brandID, modelID string, ...) ([]Year, error)
- func (c *Client) YearsByBrand(ctx context.Context, vt VehicleType, brandID string, opts ...RequestOption) ([]Year, error)
- func (c *Client) YearsByFipeCode(ctx context.Context, vt VehicleType, fipeCode string, opts ...RequestOption) ([]Year, error)
- type Model
- type Option
- type PriceHistory
- type Reference
- type RequestOption
- type Vehicle
- type VehicleType
- type Year
Examples ¶
Constants ¶
const DefaultBaseURL = "https://fipe.api.br/api/v2"
DefaultBaseURL is the production endpoint of the FIPE API.
Variables ¶
var ( ErrNotFound = errors.New("fipe: not found") ErrTooManyRequests = errors.New("fipe: too many requests") )
Sentinel errors for common API failures. APIError unwraps to these, so callers can match with errors.Is regardless of which form they prefer.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a FIPE API client. Create one with New; the zero value is not usable.
func New ¶
New returns a Client configured with the given options.
Example ¶
package main
import (
"context"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func main() {
// The free tier needs no token; pass one for higher rate limits.
client := fipe.New(fipe.WithSubscriptionToken("your-token"))
brands, err := client.Brands(context.Background(), fipe.Cars)
if err != nil {
log.Fatal(err)
}
fmt.Println(brands[0].Name)
}
Output:
func (*Client) Brands ¶
func (c *Client) Brands(ctx context.Context, vt VehicleType, opts ...RequestOption) ([]Brand, error)
Brands lists the brands available for a vehicle type.
Example ¶
package main
import (
"context"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func main() {
client := fipe.New()
brands, err := client.Brands(context.Background(), fipe.Cars)
if err != nil {
log.Fatal(err)
}
for _, b := range brands {
fmt.Println(b.Code, b.Name)
}
}
Output:
func (*Client) HistoryByFipeCode ¶
func (c *Client) HistoryByFipeCode(ctx context.Context, vt VehicleType, fipeCode, yearID string, opts ...RequestOption) (*Vehicle, error)
HistoryByFipeCode returns the vehicle details including its price history across reference months.
Example ¶
package main
import (
"context"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func main() {
client := fipe.New()
vehicle, err := client.HistoryByFipeCode(context.Background(), fipe.Cars, "005340-6", "2014-3")
if err != nil {
log.Fatal(err)
}
for _, h := range vehicle.PriceHistory {
fmt.Println(h.Month, h.Price)
}
}
Output:
func (*Client) Models ¶
func (c *Client) Models(ctx context.Context, vt VehicleType, brandID string, opts ...RequestOption) ([]Model, error)
Models lists the models of a brand.
func (*Client) ModelsByBrandYear ¶
func (c *Client) ModelsByBrandYear(ctx context.Context, vt VehicleType, brandID, yearID string, opts ...RequestOption) ([]Model, error)
ModelsByBrandYear lists the models of a brand available for a given year.
func (*Client) References ¶
References lists the FIPE monthly reference tables, newest first.
func (*Client) Vehicle ¶
func (c *Client) Vehicle(ctx context.Context, vt VehicleType, brandID, modelID, yearID string, opts ...RequestOption) (*Vehicle, error)
Vehicle returns the FIPE price details for a brand, model and year.
Example ¶
package main
import (
"context"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func main() {
client := fipe.New()
ctx := context.Background()
// Drill down brand -> model -> year -> price.
models, err := client.Models(ctx, fipe.Cars, "59")
if err != nil {
log.Fatal(err)
}
years, err := client.Years(ctx, fipe.Cars, "59", models[0].Code)
if err != nil {
log.Fatal(err)
}
vehicle, err := client.Vehicle(ctx, fipe.Cars, "59", models[0].Code, years[0].Code)
if err != nil {
log.Fatal(err)
}
fmt.Println(vehicle.Model, vehicle.Price)
}
Output:
func (*Client) VehicleByFipeCode ¶
func (c *Client) VehicleByFipeCode(ctx context.Context, vt VehicleType, fipeCode, yearID string, opts ...RequestOption) (*Vehicle, error)
VehicleByFipeCode returns the FIPE price details for a vehicle by its FIPE code and year.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func main() {
client := fipe.New()
vehicle, err := client.VehicleByFipeCode(context.Background(), fipe.Cars, "005340-6", "2014-3")
if errors.Is(err, fipe.ErrNotFound) {
log.Fatal("unknown FIPE code or year")
} else if err != nil {
log.Fatal(err)
}
fmt.Println(vehicle.Price, vehicle.ReferenceMonth)
}
Output:
func (*Client) Years ¶
func (c *Client) Years(ctx context.Context, vt VehicleType, brandID, modelID string, opts ...RequestOption) ([]Year, error)
Years lists the model-year variants of a model.
func (*Client) YearsByBrand ¶
func (c *Client) YearsByBrand(ctx context.Context, vt VehicleType, brandID string, opts ...RequestOption) ([]Year, error)
YearsByBrand lists all model years available for a brand.
func (*Client) YearsByFipeCode ¶
func (c *Client) YearsByFipeCode(ctx context.Context, vt VehicleType, fipeCode string, opts ...RequestOption) ([]Year, error)
YearsByFipeCode lists the model-year variants of a vehicle by its FIPE code (e.g. "005340-6").
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBaseURL ¶
WithBaseURL overrides the API base URL (e.g. for testing).
func WithHTTPClient ¶
WithHTTPClient sets the *http.Client used for requests.
func WithSubscriptionToken ¶
WithSubscriptionToken sets the X-Subscription-Token header on every request. The free tier works without a token but has stricter rate limits.
type PriceHistory ¶
type PriceHistory struct {
Month string `json:"month"`
Price string `json:"price"`
Reference string `json:"reference"`
}
PriceHistory is one entry of a vehicle's price across reference months.
type Reference ¶
Reference is a FIPE monthly reference table. Prices are published per reference month; pass a Reference code via WithReference to query historical tables.
type RequestOption ¶
RequestOption configures a single API request.
func WithReference ¶
func WithReference(code int) RequestOption
WithReference queries a specific FIPE monthly reference table instead of the current one. Codes come from Client.References.
Example ¶
package main
import (
"context"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func main() {
client := fipe.New()
ctx := context.Background()
// Query prices from a past monthly reference table.
refs, err := client.References(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("latest table:", refs[0].Month)
brands, err := client.Brands(ctx, fipe.Cars, fipe.WithReference(308))
if err != nil {
log.Fatal(err)
}
fmt.Println(len(brands))
}
Output:
type Vehicle ¶
type Vehicle struct {
Brand string `json:"brand"`
CodeFipe string `json:"codeFipe"`
Fuel string `json:"fuel"`
FuelAcronym string `json:"fuelAcronym"`
Model string `json:"model"`
ModelYear int `json:"modelYear"`
Price string `json:"price"`
PriceHistory []PriceHistory `json:"priceHistory,omitempty"`
ReferenceMonth string `json:"referenceMonth"`
VehicleType int `json:"vehicleType"`
}
Vehicle holds the FIPE price details for a specific model year.
type VehicleType ¶
type VehicleType string
VehicleType identifies the category of vehicle being queried.
const ( Cars VehicleType = "cars" Motorcycles VehicleType = "motorcycles" Trucks VehicleType = "trucks" )
Directories
¶
| Path | Synopsis |
|---|---|
|
Command examples demonstrates the FIPE SDK against the live API: it lists car brands, drills into VW Amarok models and years, and prints the current FIPE price.
|
Command examples demonstrates the FIPE SDK against the live API: it lists car brands, drills into VW Amarok models and years, and prints the current FIPE price. |