Smartcar Rust SDK
Rust crate for Smartcar API
Overview
Smartcar API lets you read vehicle data and send commands to vehicles (lock, unlock) using HTTP requests.
To make requests to a vehicle from a web or mobile application, the end user must connect their vehicle using Smartcar Connect. This flow follows the OAuth spec and will return a code which can be used to obtain an access token from Smartcar.
The Smartcar Rust SDK provides methods to:
- Generate the link to redirect to Connect.
- Make a request to Smartcar with the
codeobtained from Connect to obtain an access and refresh token - Make requests to the Smartcar API to read vehicle data and send commands to vehicles using the access token obtained in step 2.
Before integrating with Smartcar's SDK, you'll need to register an application in the Smartcar Developer portal. If you do not have access to the dashboard, please request access.
Installation
Add this to your Cargo.toml:
[dependencies]
smartcar = "0.1.3"
Flow
- Create a new
AuthClientstruct with yourclient_id,client_secret, andredirect_uri. - Redirect the user to Smartcar Connect using
<AuthClient>.get_auth_urlwith requiredscopeor with one of our frontend SDKs. - The user will login, and then accept or deny your
scope's permissions. - Handle the get request to your
redirect_uri.- If the user accepted your permissions:
- Use
<AuthClient>.exchange_codewith this code to obtain an access struct. This struct contains an access token (lasting 2 hours) and a refresh token (lasting 60 days).- (Save this access struct)
- Use
- If the user accepted your permissions:
- Get the user's vehicles with
get_vehicles. - Create a new
vehiclestruct using andfrom the previous response, and theaccess_token. - Make requests to the Smartcar API.
- Use
<AuthClient>.exchange_refresh_tokenon your savedrefresh_tokento retrieve a new token when youraccessTokenexpires.
Getting Started
The following code is in /examples. To run the code, replace the fake Smartcar credentials in
get_auth_client with your own and run this command:
cargo run --example=getting-started
use Query;
use StatusCode;
use ;
use Json;
use ;
use Deserialize;
use json;
use smartcar;
use ;
use Vehicle;
use ;
async
/// Helper for creating an Auth Client instance with your credentials
/// Redirect to Smartcar Connect
async
/// The potential query codes after user goes through Smartcar Connect
// Handle Smartcar callback with auth code
async
async