Backend SDK Tutorials
In this tutorial, we will go over how to integrate Connect into your application and make requests to a vehicle using our backend SDKs.
Overview
- The Application redirects the user to Smartcar Connect to request access to the user’s vehicle. In Connect, the user logs in with their vehicle credentials and grants the Application access to their vehicle.
- The user’s browser is redirected to a specified
REDIRECT_URI
. The Application Server, which is listening at theREDIRECT_URI
, will retrieve the authorization code from query parameters sent to theREDIRECT_URI
. - The Application sends a request to the Smartcar API. This request contains the authorization
code
along with the Application’sCLIENT_ID
andCLIENT_SECRET
. - In response, Smartcar returns an
ACCESS_TOKEN
and aREFRESH_TOKEN
. - Using the
ACCESS_TOKEN
, the Application can now send requests to the Smartcar API. It can access protected resources and send commands to and from the user’s vehicle via the backend service.
Prerequisites
- Sign up for a Smartcar account.
- Make a note of your
CLIENT_ID
andCLIENT_SECRET
from the Configuration section on the Dashboard. - Add the following
REDIRECT_URI
to your application configuration:http://localhost:8000/exchange
Setup
-
Clone the repo for the SDK you want to use and install the required dependencies:
-
You will also have to set the following environment variables
If you’ve used one of our frontend SDKs to integrate connect, you’ll want to set the
SMARTCAR_REDIRECT_URI
environment variable to the URI you used for that application.
If you are using Windows, ensure you are appropriately setting environment variables for your shell. Please refer to this post which details how to set environment variables on Windows.
Build your Connect URL
-
Instantiate a
Smartcar
object in the constructor of the App component.
Feel free to set mode
to simulated
or live
where you instantiate your Smartcar
object to
connect to a simulated or real vehicle.
-
A Server-side rendered application will redirect to Smartcar Connect to request access to a user’s vehicle. On Connect, the user logs in with the username and password for their vehicle’s connected services account and grants the application access to their vehicle.
To launch Connect, we need to redirect the user to the appropriate URL. We can make use of the
AUTHORIZATION_URL
function in our Smartcar object and redirect the user to the URL to launch the Connect flow.
Handle the response
Once a user has authorized the application to access their vehicle, the user is redirected to the REDIRECT_URI
with an authorization code
as a query parameter. In the previous section, we had set our REDIRECT_URI
as localhost:8000/exchange
.
Now, our server can be set up as follows to receive the authorization code
.
Launching Connect
Let’s try authenticating a vehicle! Restart your server, open up your browser and go to http://localhost:8000/login
This tutorial configures Connect to launch in test
mode by default.
In test
mode, any username
and password
is valid for each brand.
Smartcar showcases all the permissions your application is asking for - read_vehicle_info
in this case.
Once you have logged in and accepted the permissions, you should see your authorization code
printed to your console.
Getting your first access token
If you’ve used one of our frontend SDKs to integrate Connect, this is where you’ll need to fetch your access token.
In the previous section, we retrieved an authorization code
. The application must exchange the code for an ACCESS_TOKEN
to make a request.
After receiving the ACCESS_TOKEN, the user can be redirected to the /vehicle
route.
Getting data from a vehicle
-
Once the backend service or server-side application has the
ACCESS_TOKEN
, it can send requests to a vehicle using the Smartcar API. First we’ll need to fetch thevehicle_id
s associated with theACCESS_TOKEN
, then fetch vehicle attributes for one of them. After receiving thevehicle_attributes
object, we can render it in a simple table on the page. -
Restart your sever and head back to
http://localhost:8000/login
to go through Connect and make your first API request!