SDKs overview

Smartcar provides a suite of Software Development Kits (SDKs) designed to streamline the integration of its connected car API into various applications. These SDKs abstract away the complexities of direct API interaction, such as OAuth 2.0 authorization flows and HTTP request handling, allowing developers to focus on application logic. The primary SDK offering is the Connect SDK, which facilitates the user authorization process for linking vehicles to an application. Beyond Connect, Smartcar offers client libraries in several popular programming languages, each tailored to simplify API calls for retrieving vehicle data or sending commands.

The SDKs support core Smartcar functionalities, including retrieving vehicle information (e.g., odometer, fuel level, battery status), location data, and executing commands like locking/unlocking doors or charging electric vehicles. By standardizing the interaction with diverse vehicle brands and models through a unified API, the SDKs aim to reduce development time and effort for applications in areas such as fleet management, car sharing, and usage-based insurance. Developers can access comprehensive documentation and SDK guides through the Smartcar developer documentation.

Official SDKs by language

Smartcar maintains official SDKs for several programming languages, providing robust and supported tools for integration. These SDKs are actively developed and maintained by Smartcar, ensuring compatibility with the latest API versions and features. Each SDK is designed to offer an idiomatic interface for its respective language, simplifying common tasks such as authentication, making API requests, and handling responses.

The following table outlines the official SDKs, their typical package names, installation methods, and general maturity:

Language Package/Module Name Installation Command Maturity
JavaScript @smartcar/smartcar-javascript npm install @smartcar/smartcar-javascript Stable
Python smartcar pip install smartcar Stable
Node.js smartcar npm install smartcar Stable
Java com.smartcar.sdk:smartcar-java-sdk Maven/Gradle dependency Stable
Ruby smartcar gem install smartcar Stable
PHP smartcar/php-sdk composer require smartcar/php-sdk Stable

These SDKs are designed to integrate with the Smartcar API reference, providing access to endpoints for vehicle information, commands, and webhooks. Developers are encouraged to consult the specific SDK documentation for detailed usage instructions and examples.

Installation

Installation of Smartcar SDKs typically follows the standard package management practices for each respective programming language. Below are common installation commands for the official SDKs:

  • JavaScript (Browser/Frontend):
    The JavaScript SDK is often included via a script tag or bundled with modern frontend frameworks. For direct npm installation in a frontend project:
    npm install @smartcar/smartcar-javascript
    Or, for a CDN:
    <script src="https://cdn.jsdelivr.net/npm/@smartcar/smartcar-javascript@latest/dist/smartcar.min.js"></script>
  • Python:
    Install using pip, Python's package installer:
    pip install smartcar
  • Node.js (Backend):
    Install using npm, the Node.js package manager:
    npm install smartcar
  • Java:
    For Maven projects, add the following dependency to your pom.xml:
    <dependency>
        <groupId>com.smartcar.sdk</groupId>
        <artifactId>smartcar-java-sdk</artifactId>
        <version>YOUR_VERSION</version>
    </dependency>
    For Gradle projects, add to your build.gradle:
    implementation 'com.smartcar.sdk:smartcar-java-sdk:YOUR_VERSION'
    Replace YOUR_VERSION with the latest stable version available in the Smartcar Java SDK documentation.
  • Ruby:
    Install using RubyGems:
    gem install smartcar
    Then, require it in your Ruby application:
    require 'smartcar'
  • PHP:
    Install using Composer:
    composer require smartcar/php-sdk
    Ensure Composer's autoloader is included in your project:
    require 'vendor/autoload.php';

After installation, developers will typically configure the SDK with client credentials (Client ID and Client Secret) obtained from their Smartcar developer dashboard to authenticate API requests. The OAuth 2.0 authorization framework is used by Smartcar for secure access delegation, which is often handled internally by the SDKs or through the Connect SDK.

Quickstart example

This quickstart example demonstrates how to use the Smartcar Node.js SDK to initiate the Smartcar Connect flow and retrieve vehicle data. This involves setting up a basic server to handle the OAuth 2.0 redirect and then making an API call.

Prerequisites: Node.js and npm installed, a Smartcar developer account with a registered application, and your Client ID and Client Secret.

1. Install the Node.js SDK:

npm install express smartcar dotenv

2. Create a .env file:

CLIENT_ID=YOUR_SMARTCAR_CLIENT_ID
CLIENT_SECRET=YOUR_SMARTCAR_CLIENT_SECRET
REDIRECT_URI=http://localhost:8000/exchange
PORT=8000

3. Create app.js:

require('dotenv').config();
const express = require('express');
const smartcar = require('smartcar');

const app = express();
const port = process.env.PORT || 8000;

const client = new smartcar.AuthClient({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
  redirectUri: process.env.REDIRECT_URI,
  scope: ['read_vehicle_info', 'read_location', 'read_odometer'],
  testMode: true, // Set to false for production
});

app.get('/login', (req, res) => {
  const authUrl = client.get  AuthUrl();
  res.redirect(authUrl);
});

app.get('/exchange', async (req, res) => {
  const code = req.query.code;

  try {
    const access = await client.exchangeCode(code);
    const accessToken = access.accessToken;

    const vehicles = await smartcar.getVehicles(accessToken);
    const vehicleIds = vehicles.vehicles;

    // Assuming one vehicle for simplicity
    const vehicle = new smartcar.Vehicle(vehicleIds[0], accessToken);

    const attributes = await vehicle.attributes();
    const odometer = await vehicle.odometer();
    const location = await vehicle.location();

    res.json({
      message: 'Vehicle data retrieved successfully!',
      attributes: attributes,
      odometer: odometer,
      location: location,
    });
  } catch (err) {
    console.error(err);
    res.status(500).send('Error exchanging code or retrieving vehicle data.');
  }
});

app.listen(port, () => {
  console.log(`Smartcar app listening at http://localhost:${port}`);
  console.log(`Visit http://localhost:${port}/login to start the flow.`);
});

4. Run the application:

node app.js

Navigate to http://localhost:8000/login in your browser to initiate the Smartcar Connect flow. After authorizing a vehicle, you will be redirected back to /exchange, and your application will display the retrieved vehicle data. This example illustrates the basic steps of authenticating a user and making an API call to a connected vehicle, a pattern common across various Smartcar SDKs.

Community libraries

While Smartcar provides official SDKs for widely used programming languages, the open-source community may also develop and maintain unofficial libraries or wrappers. These community-contributed tools can sometimes offer support for niche languages, specific frameworks, or alternative approaches to interacting with the Smartcar API. Developers often create such libraries to fill gaps, provide specialized functionality, or integrate Smartcar with particular ecosystem tools not directly covered by official SDKs.

When considering community libraries, it is important to evaluate their maintenance status, documentation quality, and compatibility with the latest Smartcar API versions. Unlike official SDKs, community projects may not receive the same level of ongoing support or security audits. Developers should review the source code and community activity (e.g., GitHub issues, pull requests) to assess reliability before incorporating them into production systems. For the most up-to-date and officially supported integration methods, refer to the official Smartcar documentation.