Q: Where can I get a token for the Terra iOS SDK

AfraidButterfly1 month ago

The docs on setting up the Terra iOS SDK says I need a token in order to call the initConnection method in this guide. How can I get this token? I don't see it available in the dashboard...

Thanks in advance

terra

Alexadmin1 month ago

Hey

The token needed to initialise the Terra iOS SDK must be generated by making a POST request to the generateAuthToken API endpoint from your backend server, then passed into the frontend (the mobile device). This flow ensures that users are authenticated securely and your private API keys are not exposed. Also, each token has an expiry time of 180 seconds (3 minutes)

Implement a backend endpoint that looks something like this:

@app.get("/api/token") def generate_auth_token(request: Request): # ... # verify that the user making this request is signed into your app req = requests.post("https://api.tryterra.co/v2/auth/generateAuthToken", headers={ "dev-id": "<YOUR-DEV-ID>", "x-api-key": "<YOUR-API-KEY>" }) if not req.ok: return { "error": "Failed to generate token" } return req.json()

Then, use the token in your mobile app like so:

var request = URLRequest(url: URL(string: "<YOUR-BACKEND-URL>/api/token")!, timeoutInterval: Double.infinity) request.httpMethod = "POST" let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data else { return } print(String(data: data, encoding: .utf8)!) let json = try? JSONSerialization.jsonObject(with: data, options: []) if let json = json as? [String: Any] { let token = json["token"] // then use this token when calling initConnection } } task.resume()