Skip to content

tagevm/tesla-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Workflow Status Release Version Nuget Version Nuget Downloads

Buying a Tesla? Get 1,000 miles of free supercharging with my referral code.

C# Tesla API Client

This is an unofficial .NET 5 client implementation of the Tesla JSON API used by the Android and iOS apps. The API provides functionality to monitor and control the Tesla vehicles remotely.

Tim Dorr API Documentation

API Documentation GitHub

Installation

From Powershell

Nuget-Install 'Tesla-API'

.NET CLI

dotnet add package 'Tesla-API'

Setup

Setup Dependency Injection

In the Startup.cs file, add the following to the ConfigureServices method to allow the TeslaAPI to be dependency injected.

services.AddScoped<ITeslaAPI, TeslaAPI>();

Making a Request

To make a request with the Tesla API, you'll need to create a HttpClient and set the User-Agent header to an identifier for your application.

Authenticating with Tesla

Follow the standard OAuth process as documented by Tim Dorr to get an access token. After getting an access token, add it to the Authorization header on the HttpClient, which is passed into data API calls.

You can use the TeslaAuth package that provides a .net implementation to obtain a (refresh) token.

Example

public class TeslaService
{
	private readonly ITeslaAPI _teslaAPI;
	private readonly HttpClient _client = new HttpClient();

	/// <summary>
	/// Initializes a new instance of the <see cref="TeslaService"/> class.
	/// </summary>
	/// <param name="teslaClient">The TeslaAPI client.</param>
	public TeslaService(ITeslaAPI teslaAPI)
	{
	    _teslaAPI = teslaAPI;
	    _client.DefaultRequestHeaders.Add("User-Agent", "MyApplication");
	}

	/// <summary>
	/// Get all Vehicles in the user's account.
	/// </summary>
	/// <returns>Returns a list of all Vehicles.</returns>
	public async Task<List<Vehicle>> GetVehiclesAsync(string clientID, string clientSecret, string bearerToken)
	{
	    TeslaAccessToken accessToken = await _teslaAPI.GetAccesTokenAsync(_client, clientID, clientSecret, bearerToken);
	    _client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.AccessToken}");
	    return await _teslaAPI.GetAllVehiclesAsync(_client);
	}
}

About

An unofficial .NET implementation of the Tesla API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%