Skip to main content

Overview


  1. 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.
  2. The user’s browser is redirected to a specified REDIRECT_URI. The Application Server, which is listening at the REDIRECT_URI, will retrieve the user_id and state from query parameters sent to the REDIRECT_URI.
  3. The Application authenticates with the Smartcar API using the OAuth 2.0 Client Credentials flow, exchanging its CLIENT_ID and CLIENT_SECRET for an application-level access token.
  4. Using the access token and the sc-user-id header, 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 and CLIENT_SECRET from the Configuration section on the Dashboard.
  • Add the following REDIRECT_URI to your application configuration: http://localhost:8000/exchange

Setup

  1. Clone the repo for the SDK you want to use and install the required dependencies:
    $ git clone https://github.com/smartcar/getting-started-express.git
    $ cd getting-started-express/tutorial
    $ npm install
    
  2. 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.
$export SMARTCAR_CLIENT_ID=<your-client-id>
$export SMARTCAR_CLIENT_SECRET=<your-client-secret>
$export SMARTCAR_REDIRECT_URI=http://localhost:8000/exchange
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

  1. Instantiate a Smartcar object in the constructor of the App component.
    const client = new smartcar.AuthClient({
        mode: 'simulated',
    });
    
Feel free to set mode to simulated or live where you instantiate your Smartcar object to connect to a simulated or real vehicle.
  1. 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.
    app.get('/login', function(req, res) {
        const scope = ['read_vehicle_info'];
        const authUrl = client.getAuthUrl(scope);
    
        res.render('home', {
            url: authUrl,
        });
    });
    

Handle the response

Once a user has authorized the application to access their vehicle, the user is redirected to the REDIRECT_URI with a user_id and state as query parameters. 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 and store the user_id.
app.get('/exchange', function(req, res) {
const userId = req.query.user_id;

// Store the user_id — you'll use this as the sc-user-id header
// when making API requests on behalf of this user
console.log('User ID:', userId);

res.sendStatus(200);
});

Launching Connect

Let’s try authenticating a vehicle! Restart your server, open up your browser and go to http://localhost:8000/login
$node index.js

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 application access token

Smartcar uses the OAuth 2.0 Client Credentials flow for API authentication. Your application obtains a single access token that works across all connected vehicles.
Your application needs an access token to make API requests. Unlike the per-vehicle token flow, this token is obtained at the application level and works for all vehicles. The token is valid for 1 hour — simply request a new one when it expires. There is no refresh token. After obtaining the token, store the user_id from the Connect redirect and redirect to the /vehicle route.
// Global variable to save our access token and user_id
let accessToken;
let storedUserId;

// Function to get an application-level access token
async function getAccessToken() {
    const response = await fetch('https://iam.smartcar.com/oauth2/token', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: new URLSearchParams({
            grant_type: 'client_credentials',
            client_id: process.env.SMARTCAR_CLIENT_ID,
            client_secret: process.env.SMARTCAR_CLIENT_SECRET,
        }),
    });
    const data = await response.json();
    return data.access_token;
}

app.get('/exchange', async function(req, res) {
    // Store the user_id from the Connect redirect
    storedUserId = req.query.user_id;

    // Get an application-level access token
    accessToken = await getAccessToken();
    res.redirect('/vehicle');
});

Getting data from a vehicle

  1. Once the backend service has an application-level access token and the user_id, it can send requests to a vehicle using the Smartcar API. First we’ll need to fetch the connections for this user, then fetch vehicle attributes. After receiving the vehicle attributes, we can render them in a simple table on the page.
    app.get('/vehicle', async function(req, res) {
        // Use the Connections API to get vehicle IDs for this user
        const connectionsResponse = await fetch(
            'https://vehicle.api.smartcar.com/v3/connections',
            {
                headers: {
                    'Authorization': `Bearer ${accessToken}`,
                    'sc-user-id': storedUserId,
                },
            }
        );
        const { connections } = await connectionsResponse.json();
    
        // Get vehicle info for the first connected vehicle
        const vehicleResponse = await fetch(
            `https://vehicle.api.smartcar.com/v3/vehicles/${connections[0].vehicleId}`,
            {
                headers: {
                    'Authorization': `Bearer ${accessToken}`,
                    'sc-user-id': storedUserId,
                },
            }
        );
        const attributes = await vehicleResponse.json();
        res.render('vehicle', {
            info: attributes,
        });
    });
    
  2. Restart your sever and head back to http://localhost:8000/login to go through Connect and make your first API request!
    $node index.js