Overview

- The Single-Page Application launches a pop-up window with Smartcar Connect to request access to a user’s vehicle. It does so by using the Smartcar JavaScript SDK. 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.
- The user’s browser is redirected to a specified Smartcar-hosted
REDIRECT_URI- the Smartcar JavaScript SDK redirect page. - The Smartcar JavaScript SDK redirect page will receive a
user_idandstateas query parameters. The redirect page will then send these to the Single-Page Application’sonCompletecallback using thepostMessageweb API. This step is handled entirely by the JavaScript SDK. - The Single-Page Application sends the received
user_idto the Application’s backend service for storage. - The Application’s backend authenticates with the Smartcar API using the OAuth 2.0 Client Credentials flow to obtain an application-level access token.
- Using the access token and the
sc-user-idheader, 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_IDandCLIENT_SECRETfrom the Configuration section on the Dashboard. - Add the special JavaScript SDK redirect URI to your application configuration.
- Add the
REACT_APP_SERVERredirect URI from Setup step 2. to your application configuration.
To use the JavaScript SDK, we require the special redirect URI to provide a simpler flow to retrieve authorization codes.
For this tutorial you can use the following:
https://javascript-sdk.smartcar.com/v2/redirect?app_origin=http://localhost:3000Setup
- Clone our repo and install the required dependencies:
- Set the following environment variables. We will be setting up a server later for our React front-end to communicate with.
For now, let’s assume our server is listening on
http://localhost:8000.
Note: 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
Smartcarobject in theconstructorof the App component.App.jsx
This tutorial uses
test mode by default. Feel free to set mode to live where you instantiate your Smartcar object to
connect to a real vehicle, or simulated to connect to a simulated one.-
A single-page application will launch a pop-up window with 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 can use the
openDialogfunction that ourSmartcarobject has access to. We can place this is anonClickhandler of an HTML button.App.jsx
Handle the response
- Once a user has authorized the application to access their vehicle, the user is redirected to the
redirect_uriwith an authorization code as a query parameter and the pop-up dialog is closed. Using the JavaScript SDK, the frontend can receive thiscodein theonCompletecallback passed into theSmartcarobject.App.jsx
Launching Connect
Restart your server, open up your browser and go tohttp://localhost:3000/login. You should be reciderect to Smartcar Connect.
This tutorial configures Connect to launch in
test mode by default.
In test mode, any username and password is valid for each brand.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.
Sending the user_id to your backend
After the user completes the Connect flow, the React application receives auser_id. Send this to your backend service for storage — your backend will use it as the sc-user-id header when making API requests on behalf of this user.
We can add this logic in our onComplete callback:
App.jsx
Getting data from a vehicle
Once your backend has theuser_id and an application-level access token, it can send requests to a vehicle using the Smartcar API. The React app will
have to send a request to the backend service which in turn sends a request to Smartcar. We have to do this because
our frontend does not have the access token.
Assuming our backend has a /vehicle endpoint that returns the information of a user’s vehicle, we can make this query in
our completion callback and set the state of the React app with the returned vehicle attributes.
App.jsx
App.jsx
Setting up your backend
Now that our frontend is complete, we will need to create a backend service that stores theuser_id and handles API authentication. Your backend will use the OAuth 2.0 Client Credentials flow to obtain an application-level access token and make requests to vehicles.
You can use any of our backend SDKs below to set up the service.
When setting up the environment variables for your backend SDK, make sure to set
REDIRECT_URI to the custom scheme
used for this tutorial i.e. https://javascript-sdk.smartcar.com/v2/redirect?app_origin=http://localhost:3000.
