Skip to content
Smartcar's Go SDK
Go Other
  1. Go 99.4%
  2. Other 0.6%
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
scripts
.gitignore
.travis.yml
Makefile
README.md
authentication.go
authentication_e2e_test.go
authentication_test.go
go.mod Feat: Add e2e tests (#14) Nov 1, 2019
go.sum
requests.go
requests_test.go
smartcar.go
smartcar_e2e_test.go
smartcar_test.go Feat: Add unit tests (#12) Oct 23, 2019
utils.go
utils_test.go Feat: Make utils private (#10) Oct 16, 2019
vehicle.go
vehicle_e2e_test.go Feat: Add e2e tests (#14) Nov 1, 2019
vehicle_test.go

README.md

Smartcar Go SDK

GoDoc

Overview

The Smartcar API lets you read vehicle data (location, odometer, fuel, etc.) and send commands to vehicles (lock, unlock) using HTTP requests.

Installation

Install the smartcar package if you are not using Go modules:

go get -u github.com/smartcar/go-sdk

Import it using:

import smartcar "github.com/smartcar/go-sdk"

Getting Started Guide

  1. Initialize a Smartcar Client.

    // A smartcar Client is needed to talk to any of the methods in the SDK
    smartcarClient := smartcar.NewClient()
  2. Initialize an Auth Client struct with your client id, client secret, redirect URI, the scopes you want, and test mode.

    // An Auth Client is used to generate a smartcar connect url, authenticate with smartcar, and check compatibility
    authClient := smartcarClient.NewAuth(&smartcar.AuthParams{
    	ClientID:     "<CLIENT_ID>",
    	ClientSecret: "<CLIENT_SECRET>",
    	RedirectURI:  "<REDIRECT_URI>",
    	Scope:        []string{"read_vehicle_info"},
    	TestMode:     true,
    })
  3. Get an auth URL and then redirect user to that URL.

    authURL, err := authClient.GetAuthURL(&smartcar.AuthURLParams{})
    
    // redirect user here.
    
    /*
    	If using the net/http library, you can use http.Redirect.
    	In order for the next line to work you need to have an endpoint that
    	has access to a http.ResponseWriter and http.Request.
    */
    http.Redirect(w, req, authURL, http.StatusSeeOther)
  4. Setup up a redirectURI endpoint to receive authorization code. Exchange auth code for an authorization.

    // Exchange initial authorization code
    token, err := authClient.ExchangeCode(
    	context.TODO(),
    	&smartcar.ExchangeCodeParams{Code: code},
    )
    
    // When your token expires, you can exchange it by sending your refresh token for continued access.
    token, err := authClient.ExchangeRefreshToken(
    	context.TODO(),
    	&smartcar.ExchangeRefreshTokenParams{Token: token.Refresh},
    )
    
    // You can check the validity of your token by callint the IsTokenExpired method
    isExpired := smartcarClient.IsTokenExpired(&smartcarClient.TokenExpiredParams{
    	Expiry: token.AccessExpiry,
    })
  5. In order to send a request to a vehicle, you need to create a smartcar.Vehicle, and for that you need a vehicle ID. You can get a vehicleID by sending a request to smartcar.GetVehicleIDs, which will return a list of vehicleIDs associated with an access token.

    vehicleIDs, err := smartcar.GetVehicleIDs(
    	context.TODO(),
    	&smartcar.VehicleIDsParams{Access: token.Access},
    )
  6. Construct vehicle with an ID, and an AccessToken, a UnitSystem is optional

    vehicleParams := smartcarClient.VehicleParams{
    	ID: (*vehicleIDs)[0],
    	AccessToken: token.Access,
    }
    vehicle := smartcarClient.NewVehicle(&vehicleParams)
  7. Send request to vehicle. The following endpoints are available. (check the go documentation for the most up to date list.)

    // Vehicle Endpoints
    battery, err := vehicle.GetBattery(context.TODO())
    charge, err := vehicle.GetCharge(context.TODO())
    disconnect, err := vehicle.Disconnect(context.TODO())
    fuel, err := vehicle.GetFuel(context.TODO())
    info, err := vehicle.GetInfo(context.TODO())
    location, err := vehicle.GetLocation(context.TODO())
    lock, err := vehicle.Lock(context.TODO())
    odometer, err := vehicle.GetOdometer(context.TODO())
    oil, err := vehicle.GetOil(context.TODO())
    permissions, err := vehicle.GetPermissions(context.TODO())
    tirePressure, err := vehicle.GetTiresPressue(context.TODO())
    unlock, err := vehicle.Unlock(context.TODO())
    vin, err := vehicle.GetVIN(context.TODO())
    
    // You can change the unit systems of a vehicle at any point by doing.
    err := vehicle.SetUnits(smartcar.UnitsParams{Unit: smartcar.UnitSystemMetric})

Pro Features

Compatibility

Compatibility allows you to verify if a particular VIN is compatible with a scope of permissions. This method should be used prior to directing a user to the Smartcar Connect flow. Learn more on our doc center.

isCompatible, err := smartcarClient.IsVINCompatible(context.TODO(), &smartcar.VINCompatibleParams{
	VIN: "<VIN>",
	Scope: []string{"<Scope>"},
	ClientID:     "<CLIENT_ID>",
	ClientSecret: "<CLIENT_SECRET>",
})

Batch

Batch allows you to send make requests to multiple endpoints in a single request.

batch, err := vehicle.Batch(
	context.TODO(),
	smartcar.BatteryPath,
	smartcar.InfoPath,
	smartcar.LocationPath,
	smartcar.OdometerPath,
)
battery := batch.Battery
info := batch.Info
location := batch.Location
odometer := batch.Odometer
You can’t perform that action at this time.