Skip to content

parallelum/fipe-csharp

Repository files navigation

Br.Com.Parallelum.Fipe - the C# library for the Fipe API

API de Consulta Tabela FIPE fornece preços médios de veículos no mercado nacional. Atualizada mensalmente com dados extraidos da tabela FIPE.

Essa API Fipe utiliza banco de dados próprio, onde todas as requisições acontecem internamente, sem sobrecarregar o Web Service da Fipe, evitando assim bloqueios por múltiplos acessos.

A API está online desde 2015 e totalmente gratuíta. Gostaria que ele continuasse gratuíta? O que acha de me pagar uma cerveja? 🍺

Make a donation

Fipe API SDKs

This C# SDK is automatically generated by the OpenAPI Generator project:

  • API version: 2.0.0
  • SDK version: 1.0.4
  • Build package: org.openapitools.codegen.languages.CSharpNetCoreClientCodegen For more information, please visit https://github.com/deividfortuna/fipe

Frameworks supported

  • .NET Core >=1.0
  • .NET Framework >=4.6
  • Mono/Xamarin >=vNext

Dependencies

The DLLs included in the package may not be the latest version. We recommend using NuGet to obtain the latest version of the packages:

Install-Package Newtonsoft.Json
Install-Package JsonSubTypes
Install-Package System.ComponentModel.Annotations

Installation

Generate the DLL using your preferred tool (e.g. dotnet build)

Then include the DLL (under the bin folder) in the C# project, and use the namespaces:

using Br.Com.Parallelum.Fipe.Api;
using Br.Com.Parallelum.Fipe.Client;
using Br.Com.Parallelum.Fipe.Model;

Usage

To use the API client with a HTTP proxy, setup a System.Net.WebProxy

Configuration c = new Configuration();
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;

Connections

Each ApiClass (properly the ApiClient inside it) will create an instance of HttpClient. It will use that for the entire lifecycle and dispose it when called the Dispose method.

To better manager the connections it's a common practice to reuse the HttpClient and HttpClientHandler (see here for details). To use your own HttpClient instance just pass it to the ApiClass constructor.

HttpClientHandler yourHandler = new HttpClientHandler();
HttpClient yourHttpClient = new HttpClient(yourHandler);
var api = new YourApiClass(yourHttpClient, yourHandler);

If you want to use an HttpClient and don't have access to the handler, for example in a DI context in Asp.net Core when using IHttpClientFactory.

HttpClient yourHttpClient = new HttpClient();
var api = new YourApiClass(yourHttpClient);

You'll loose some configuration settings, the features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. You need to either manually handle those in your setup of the HttpClient or they won't be available.

Here an example of DI setup in a sample web project:

services.AddHttpClient<YourApiClass>(httpClient =>
   new PetApi(httpClient));

Getting Started

using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using Br.Com.Parallelum.Fipe.Api;
using Br.Com.Parallelum.Fipe.Client;
using Br.Com.Parallelum.Fipe.Model;

namespace Example
{
    public class Example
    {
        public static void Main()
        {

            Configuration config = new Configuration();
            config.BasePath = "https://parallelum.com.br/fipe/api/v2";
            // create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
            HttpClient httpClient = new HttpClient();
            HttpClientHandler httpClientHandler = new HttpClientHandler();
            var apiInstance = new FipeApi(httpClient, config, httpClientHandler);
            var vehicleType = (VehiclesType) "cars";  // VehiclesType | Type of vehicle

            try
            {
                // Brands by type
                List<NamedCode> result = apiInstance.GetBrandsByType(vehicleType);
                Debug.WriteLine(result);
            }
            catch (ApiException e)
            {
                Debug.Print("Exception when calling FipeApi.GetBrandsByType: " + e.Message );
                Debug.Print("Status Code: "+ e.ErrorCode);
                Debug.Print(e.StackTrace);
            }

        }
    }
}

Documentation for API Endpoints

All URIs are relative to https://parallelum.com.br/fipe/api/v2

Class Method HTTP request Description
FipeApi GetBrandsByType GET /{vehicleType}/brands Brands by type
FipeApi GetFipeInfo GET /{vehicleType}/brands/{brandId}/models/{modelId}/years/{yearId} Fipe info
FipeApi GetHistoryByFipeCode GET /{vehicleType}/{fipeCode}/years/{yearId}/history Fipe price history by Fipe code
FipeApi GetInfoByFipeCode GET /{vehicleType}/{fipeCode}/years/{yearId} Fipe info by Fipe code
FipeApi GetModelsByBrand GET /{vehicleType}/brands/{brandId}/models Models by brand
FipeApi GetReferences GET /references Fipe month references
FipeApi GetYearByModel GET /{vehicleType}/brands/{brandId}/models/{modelId}/years Years by model
FipeApi GetYearsByFipeCode GET /{vehicleType}/{fipeCode}/years Years by Fipe code

Documentation for Models