https://github.com/hamblinsoft/teslakit
Elegant Tesla API in Swift
https://github.com/hamblinsoft/teslakit
ios ios-lib model models modelx swift swift3 swift4 tesla tesla-api teslaapi teslakit teslamotors
Last synced: about 1 year ago
JSON representation
Elegant Tesla API in Swift
- Host: GitHub
- URL: https://github.com/hamblinsoft/teslakit
- Owner: HamblinSoft
- License: mit
- Created: 2017-12-04T09:00:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-24T10:04:10.000Z (about 6 years ago)
- Last Synced: 2025-04-07T09:46:36.330Z (about 1 year ago)
- Topics: ios, ios-lib, model, models, modelx, swift, swift3, swift4, tesla, tesla-api, teslaapi, teslakit, teslamotors
- Language: Swift
- Homepage:
- Size: 37.3 MB
- Stars: 49
- Watchers: 6
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TeslaKit
[](https://github.com/Jarious-Apps/TeslaKit)
[](https://github.com/CocoaPods/CocoaPods)
[](http://cocoapods.org/pods/TeslaKit)
[](http://cocoapods.org/pods/TeslaKit)
[](https://swift.org)
[](http://twitter.com/jjjjaren)
TeslaKit is a framework written in Swift that makes it easy for you to interface with Tesla's mobile API and communicate with your Tesla automobiles.
# Features
- [x] Authenticate with Tesla's API to obtain an access token
- [x] Retrieve a list of vehicles associated with your Tesla account
- [x] Obtain all data on your vehicle
- [x] Send commands to your vehicle
- [x] Utilizes `ObjectMapper` for `JSON` mapping
- [x] Uses `Structures` to maintain thread safe operations
- [x] VIN Decoder
- [ ] Summon and Homelink - Coming soon
# Inspiration
I have been a long time fan of Tesla ever since the original Roadster. When the Model 3 was announced, I actually camped out overnight so I could place my reservation. I am also a big Apple fan and really enjoy my Apple Watch, although wish there were more interesting complications I could add to my watch faces. So I decided to make my own app while I wait for my Model 3.
Fast foward to mid-2018, I have officially released my app, called [AutoMate for Tesla](https://apple.co/2ylLmOf), on the App Store and have also received my Model 3! During development of AutoMate, I decided to separate the core Tesla API interactions into a separate project and open source it so others could also build really cool apps too.
I am looking forward to seeing what people do with TeslaKit and if you're anything like me and thoroughly enjoy your car and own an Apple Watch, I think you will also enjoy AutoMate so please give it a try!
# Installation
TeslaKit is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'TeslaKit'
```
# Usage
Add an ATS exception domain for `owner-api.teslamotors.com` in your info.plist
```xml
NSAppTransportSecurity
NSExceptionDomains
owner-api.teslamotors.com
NSIncludesSubdomains
NSExceptionMinimumTLSVersion
TLSv1.0
NSExceptionAllowsInsecureHTTPLoads
```
Add an import statement for `TeslaKit` into your file
```swift
import TeslaKit
```
## TeslaAPI
Create a new `TeslaAPI` instance
```swift
let teslaAPI = TeslaAPI()
```
Also see other initialization [configuration](#configuration) options
## Access Token
Obtain an Access Token by logging in with your Tesla account credentials
```swift
let email = "e.musk@teslakit.com"
let password = "M@R$R0X!"
teslaAPI.getAccessToken(email: email, password: password) { (httpResponse, dataOrNil, errorOrNil) in
guard let accessToken = dataOrNil?.accessToken else { return }
// Set the accessToken for use with future requests
teslaAPI.setAccessToken(accessToken)
}
```
## Vehicle List
Obtain a list of vehicles associated with your account
```swift
teslaAPI.getVehicles { (httpResponse, dataOrNil, errorOrNil) in
guard let vehicle = dataOrNil?.vehicles.first else { return }
print("Hello, \(vehicle.displayName)")
}
```
## Vehicle Data
Obtain all data for a vehicle
```swift
teslaAPI.getData(vehicle.id) { (httpResponse, dataOrNil, errorOrNil) in
guard let vehicle = dataOrNil else { return }
print("Battery is at \(vehicle.chargeState.batteryLevel)%")
}
```
## Send Command
Send a command to a vehicle
```swift
let command = Command.unlockDoors
teslaAPI.send(command, to: vehicle) { response in
if response.result {
print("Command sent successfully!")
}
}
```
Send a command to a vehicle with request parameters
```swift
let parameters = SetTemperature(driverTemp: 21.0, passengerTemp: 21.0)
teslaAPI.send(.setTemperature, to: vehicle, parameters: parameters) { response in
if response.result {
print("Command sent successfully!")
}
}
```
## Default
```swift
let teslaAPI = TeslaAPI(configuration: .default) // same as TeslaAPI()
```
The ```default``` configuration points to Tesla's production owner API
## Mock
```swift
let teslaAPI = TeslaAPI(configuration: .mock)
```
The ```mock``` configuration directs all requests to a custom server that returns a predefined responses for each of the owner API endpoints allowing you to work with some real-looking data to work with without having to own a Tesla vehicle or without having a Tesla account at all.
Note: Because this is a free service, requests are occasionally throttled.
## Custom
```swift
let customConfig = TeslaAPI.Configuration(baseURL: URL(string: "SOME_URL")!,
clientId: "SOME_CLIENT_ID",
clientSecret: "SOME_CLIENT_SECRET",
requestTimeout: 30)
let teslaAPI = TeslaAPI(configuration: customConfig)
```
The ```custom``` configuration allows you to specify your own ```baseURL```. This is convenient if you would like to point to your own environment. You can also specify an alternate ```clientId``` and ```clientSecret``` value. You can also specify a different request timeout interval (default: ```30```)
## Debug Mode
```swift
let teslaAPI = TeslaAPI(debugMode: true)
```
Enabling debug mode will print all the raw request and response information to the console (default: ```false```)
# VIN Decoder
```swift
let vin = VIN(vinString: "5YJSA1E2XHF999999")!
print(vin.make) // Model S
print(vin.driveUnit) // Dual Motor
print(vin.modelYear) // 2017
print(vin.serialNo) // 999999
print(vin.vinString) // 5YJSA1E2XHF999999
```
# Apps Built With TeslaKit
- [AutoMate for Tesla](https://apple.co/2ylLmOf)
Is your app using TeslaKit? Let me know and I'll feature it here too!
# Tesla Referral Program
Buying a new Tesla? Use my [referral code](https://ts.la/jaren29211) and receive 1,000 free Supercharger miles!
# Author
jjjjaren, jjjjaren@gmail.com
# License
This project is licensed under the terms of the MIT license. See the [LICENSE](LICENSE) file.
> This project is in no way affiliated with Tesla Inc. This project is open source under the MIT license, which means you have full access to the source code and can modify it to fit your own needs.