{"id":13693141,"url":"https://github.com/jonasman/TeslaSwift","last_synced_at":"2025-05-02T21:31:34.138Z","repository":{"id":39954088,"uuid":"53198002","full_name":"jonasman/TeslaSwift","owner":"jonasman","description":"Swift library to access the Tesla API","archived":false,"fork":false,"pushed_at":"2024-10-29T20:13:38.000Z","size":828,"stargazers_count":249,"open_issues_count":20,"forks_count":72,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-04-17T16:11:14.094Z","etag":null,"topics":["model3","models","modelx","swift-library","tesla","teslaapi"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonasman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-05T11:24:03.000Z","updated_at":"2025-02-23T22:46:26.000Z","dependencies_parsed_at":"2024-01-16T09:55:42.160Z","dependency_job_id":"1c3feedf-b46c-4a8a-92ae-e41260217f54","html_url":"https://github.com/jonasman/TeslaSwift","commit_stats":{"total_commits":550,"total_committers":24,"mean_commits":"22.916666666666668","dds":"0.22363636363636363","last_synced_commit":"2b350db75201734ebbc8ee3df78bbc8bc97d3f61"},"previous_names":[],"tags_count":114,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonasman%2FTeslaSwift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonasman%2FTeslaSwift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonasman%2FTeslaSwift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonasman%2FTeslaSwift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonasman","download_url":"https://codeload.github.com/jonasman/TeslaSwift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252108801,"owners_count":21696138,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["model3","models","modelx","swift-library","tesla","teslaapi"],"created_at":"2024-08-02T17:01:06.149Z","updated_at":"2025-05-02T21:31:33.755Z","avatar_url":"https://github.com/jonasman.png","language":"Swift","funding_links":[],"categories":["API Wrappers","Swift"],"sub_categories":[],"readme":"# TeslaSwift\nSwift library to access Tesla API based on [Tesla JSON API (Unofficial)](https://tesla-api.timdorr.com) and [Tesla Fleet API](https://developer.tesla.com/docs/fleet-api)\n\n[![Swift](https://img.shields.io/badge/Swift-5.9-orange.svg?style=flat)](https://swift.org)\n[![Build Status](https://travis-ci.org/jonasman/TeslaSwift.svg?branch=master)](https://travis-ci.org/jonasman/TeslaSwift)\n\n## Installation\n\n### Swift Package Manager\n\nYou can use [Swift Package Manager](https://swift.org/package-manager/) and specify a dependency in `Package.swift` by adding this or adding the dependency to Xcode:\n\n```swift\n.Package(url: \"https://github.com/jonasman/TeslaSwift.git\", majorVersion: 10)\n```\n\nThere are also extensions for Combine `TeslaSwiftCombine`\n\nThe Streaming extensions are: `TeslaSwiftStreaming`, Combine `TeslaSwiftStreamingCombine` \n\n## Tesla API\nThere are 2 Tesla APIs available:\n1. The old owner API\n2. The new Fleet API\n\nYou can choose any of them. If you want to use FleetAPI, initialize the library with a `region`, `clientId`, `clientSecret` and `redirectURI`\n\n## App registration for the Fleet API\nTo use the new Fleet API, you will need to register your app.\n\nFollow the steps on the [official documentation](https://developer.tesla.com/docs/fleet-api#setup):\n1. Create a private/public key and upload the public key to a website\n2. Make a new app at [Tesla Developer](https://developer.tesla.com/dashboard)\n3. Get a partner token (using this Library)\n4. Register your app (using this Library)\n\nThis library helps you get a partner token and register your app with 2 APIs:\n```swift\ngetPartnerToken()\nregisterApp(domain: String)\n```\n\n## Usage\n\nTesla's server is not compatible with ATS so you need to add the following to your app Info.plist\n\n```XML\n\u003ckey\u003eNSAppTransportSecurity\u003c/key\u003e\n\u003cdict\u003e\n    \u003ckey\u003eNSAllowsArbitraryLoads\u003c/key\u003e\n    \u003ctrue/\u003e\n\u003c/dict\u003e\n```\n\nImport the module\n\n```swift\nimport TeslaSwift\n```\n\nAdd the extension modules if needed (with the previous line)\n\n```swift\nimport TeslaSwiftCombine\n```\n\nPerform an authentication with your MyTesla credentials using the browser:\n\nIf you use deeplinks, add your callback URI scheme as a URL Scheme to your app under info -\u003e URL Types\n```swift\n if let url = api.authenticateWebNativeURL() {\n    UIApplication.shared.open(url)\n}\n...\n\nfunc application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -\u003e Bool {\n    Task { @MainActor in\n        do {\n            _ = try await api.authenticateWebNative(url: url)\n            // Notify your code the auth is done\n        } catch {\n            print(\"Error\")\n        }\n    }        \n    return true\n}\n```   \n\nAlternative method using a webview (this method does not have auto fill for email and MFA code)\nPerform an authentication with your MyTesla credentials using the web oAuth2 flow with MFA support:\n\n```swift\nlet teslaAPI = ...\nlet api = TeslaSwift(teslaAPI: teslaAPI)\nlet (webloginViewController, result) = api.authenticateWeb()\nguard let webloginViewController else { return }\npresent(webloginViewController, animated: true, completion: nil)\nTask { @MainActor in\n        do {\n             _ = try await result()\n             self.messageLabel.text = \"Authentication success\"\n        } catch let error {\n            // error\n       }\n}\n```\n\nTo to perform an authentication in SwiftUI, create an UIViewControllerRepresentable to inject the UIViewController into a SwiftUI view:\n\n```swift\nimport TeslaSwift\nimport SwiftUI\n\nstruct TeslaWebLogin: UIViewControllerRepresentable {\n    let teslaAPI = ...\n    let api = TeslaSwift(teslaAPI: teslaAPI)    \n    \n    func makeUIViewController(context: Context) -\u003e TeslaWebLoginViewController {\n        let (webloginViewController, result) = api.authenticateWeb()        \n        Task { @MainActor in\n                do {\n                     _ = try await result()\n                    print(\"Authentication success\")                    \n                    guard api.isAuthenticated else { return }\n                    Task { @MainActor in\n                        do {\n                            let vehicles = try await api.getVehicles()\n\n                            // post process your vehicles here\n\n                        } catch {\n                            print(\"Error\",error)\n                        }\n                    }                    \n                } catch let error {\n                    print(\"Error\", error)\n               }\n        }        \n        return webloginViewController!\n    }\n    \n    func updateUIViewController(_ uiViewController: TeslaWebLoginViewController, context: Context) {\n    }\n    \n}\n```\n\n```swift\nimport SwiftUI\nstruct TeslaLogin: View {\n    var body: some View {\n        VStack {\n            TeslaWebLogin()\n        }\n    }\n}\n```\n## Public Key upload to vehicle\nAfter authenticatiom, some vehicles might need to receive your public key.\n\n```swift\nif let url = api.urlToSendPublicKeyToVehicle(domain: yourDomain) {\n    UIApplication.shared.open(url)\n}\n```\n\nThis will open the Tesla app and send the public key to the selected vehicle in the app.\n\n\n## Token reuse\nAfter authentication, store the AuthToken in a safe place.\nThe next time the app starts-up you can reuse the token:\n\n```swift\nlet teslaAPI = ...\nlet api = TeslaSwift(teslaAPI: teslaAPI)\napi.reuse(token: previousToken)\n\n```\n\n## Vehicle data\nExample on how to get a list of vehicles\n\n```swift\n\nclass CarsViewController: ViewController {\n    func showCars() {\n      do {\n        let response = try await api.getVehicles()\n        self.data = response\n        self.tableView.reloadData()\n      } catch let error {\n        //Process error\n     }\n}\n```\n\n## Streaming\nImport the module\n\n```swift\nimport TeslaSwiftStreaming\n```\n\nImport the extension modules if needed (with the previous line)\n\n```swift\nimport TeslaSwiftStreamingCombine\n```\n```swift\nclass CarsViewController: ViewController {\n\n  func showStream() {\n    stream = TeslaStreaming(teslaSwift: api)\n    do {\n        for try await event in try await stream.openStream(vehicle: myVehicle) {\n            switch event {\n                case .open:\n                    // Open\n                case .event(let streamEvent):\n                    self.data.append(streamEvent)\n                    self.tableView.reloadData()\n                case .error(let error):                    \n                    // Process error\n                case .disconnet:\n                    break\n            }\n        }\n    } catch let error {\n    // error\n    }\n\n    // After some events...\n    stream.closeStream()\n   }\n}\n```\n\n## Encoder and Decoder\n\nIf you need a JSON Encoder and Decoder, the library provides both already configured to be used with Tesla's JSON formats\n\n```swift\npublic let teslaJSONEncoder: JSONEncoder\npublic let teslaJSONDecoder: JSONDecoder\n```  \n\n## Options\n\nYou can enable debugging by setting: `api.debuggingEnabled = true`\nDebug logs use Unified Logging and can be found by filtering for `subsystem: Tesla Swift`\n\n## Other Features\n\nAfter the authentication is done, the library will manage the access token. \nWhen the token expires the library will request a new token using the refresh token.\n\n## Referral\n\nIf you want to buy a Tesla or signup for the mailing list using my referral as a \"thank you\" for this library here it is:\nhttp://ts.la/joao290\n\n## Apps using this library\n\n* Key for Tesla (https://itunes.apple.com/us/app/key-for-tesla/id1202595802?mt=8)\n* Camper for Tesla (https://itunes.apple.com/us/app/camper-for-tesla/id1227483065?mt=8)\n* Power for Tesla (https://itunes.apple.com/us/app/power-for-tesla/id1194710823?mt=8)\n* Plus - for Tesla Model S \u0026 X (https://itunes.apple.com/us/app/plus-for-tesla-model-s-x/id1187829197?mt=8)\n* Nikola for Tesla (https://itunes.apple.com/us/app/nikola-for-tesla/id1244489779?mt=8)\n* Charged - for Tesla (https://getcharged.app/)\n* TeSlate (https://infinytum.co/)\n* Companion for Tesla - oAuth - (https://teslacompanion.app)\n* Pilot Log for Tesla (https://itunes.apple.com/us/app/pilot-log-for-Tesla/id1564398488?mt=8)\n* EV Companion (https://itunes.apple.com/us/app/ev-companion/id1574209459?mt=8)\n* S3XY Key Fob (https://kenmaz.net/tesla-app/)\n* Meter (https://apps.apple.com/us/app/meter/id1603166204)\n* Chargey (https://get.chargey.app/)\n* Charge My Way (https://apple.co/42EtHM5)\n\nMissing your app? open a PR or issue\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2016 João Nunes\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonasman%2FTeslaSwift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonasman%2FTeslaSwift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonasman%2FTeslaSwift/lists"}