Smartcar Rust SDK
Rust library crate for sending requests to 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.
Note that the Rust SDK only supports version 2.0 of Smartcar API.
Installation
Add this to your Cargo.toml:
[dependencies]
smartcar = "1.0.0"
Flow
-
Create a new
AuthClientstruct with yourclient_id,client_secret, andredirect_uri. -
Redirect the user to Smartcar Connect using
<AuthClient>.get_auth_urlwith requiredscope. -
The user will login and then accept or deny your
scope's permissions. -
If the user accepted your permissions:
a. Handle the get request to your
redirect_uri. It will have a querycode, which represents the user's consent.b. Use
<AuthClient>.exchange_codewith this code to obtain anAccessstruct. This struct contains your tokens:access_token(lasting 3 hours) andrefresh_token(lasting 60 days) *. -
Use
get_vehiclesto get aVehiclesstruct that has all the ids of the owner's vehicles. -
Create a new
Vehicle(singular) struct using anidfrom the previous response and theaccess_tokenfrom Step 4. -
Start making requests to the Smartcar API!
* In order to make subsequent requests, you will need to save this the tokens in the Access struct somewhere.
** When your access token expires, use <AuthClient>.exchange_refresh_token on your refresh_token to get a fresh set.
Getting Started
Let's see a basic use case of smartcar using the axum web framework. In this example, we will set up a simple server running on localhost 3000 to run through the flow described above, in order to get the make, model, and year of a vehicle.
See the code in ./example/getting-started.rs.
For a much simpler example without a web framework integration, check out ./example/getting-started-cli.rs.
Requirements
- Rust/cargo
- A browser
How to run this example
- Clone this repo
cdinto the directory. - Set up a new redirect URI in your Smartcar dashboard.
- Add
http://localhost:3000/callback
- Add
- Find
get_auth_clientin ./example/getting-started.rs and replace the fake credentials with your actual client credentials from your dashboard.- The fake credentials are prefixed with
"REPLACE_WITH_YOUR_".
- The fake credentials are prefixed with
- Run the example by using the cargo run with the example flag:
cargo run --example=getting-started
-
In a browser, go
http://locahost:3000/loginto see the Smartcar Connect flow. This example runs connect in Test Mode, which uses randomized data and fake cars.- Normally, your users will be the one going through this flow. In this example, you will be going through it yourself.
- Choose any make and type in a fake email/password
- e.g. username:
"blah@blah.com", password:"blah"
-
After logging in and approving permissions, you should get a JSON response with the vehicle's make, model, year, and id.
Follow along with the print statements in your terminal to see the steps!
** example/getting-started.rs has print statements that correspond to the 7-step Flow above. To minimize noise, the code below does not include the print statements.
Getting started (with axum web framework)
use Query;
use StatusCode;
use ;
use Json;
use ;
use Deserialize;
use json;
use *;
use ;
use ;
use Vehicle;
async
/// Helper for creating an Auth Client instance with your credentials
/// Smartcar Connect Flow
async
async
async