Services
Static Fields
You can set your API key globally for BasisTheoryElements through the static apiKey field.
BasisTheoryElements.apiKey = "<YOUR PUBLIC BT API KEY>"
All service method calls take an optional apiKey should you need to override the globally set apiKey.
apiKey since its use case is different from the other services and requires a session API key for requests.Methods
tokenize
Elements' values can be securely tokenized utilizing our tokenize services. To accomplish this, simply pass the Element instance in the payload.
let body: [String: Any] = [
"data": [
"property": <BasisTheory Element instance>,
"myProp": "myValue"
],
"search_indexes": ["{{ data.property }}"],
"type": "token"
]
BasisTheoryElements.tokenize(body: body, apiKey: "<YOUR PUBLIC API KEY>")
{ data, error in ... }
tokenize requires the use of a public API key (an API key issued to a public Application). Click here to create one in the Basis Theory portal.The callback provided calls your function with a data of type AnyCodable, and an error of type Error.
createToken
Elements' values can be securely tokenized utilizing our createToken services. To accomplish this, simply pass the Element instance in the payload.
let body: CreateToken = CreateToken(type: "token", data: [
"property": <BasisTheory Element instance>,
"myProp": "myValue",
], searchIndexes: ["{{ data.property }}"])
BasisTheoryElements.createToken(body: body, apiKey: "<YOUR PUBLIC API KEY>")
{ data, error in ... }
createToken requires the use of a public API key (an API key issued to a public Application). Click here to create one in the Basis Theory portal.The callback provided calls your function with a data of type CreateTokenResponse, and an error of type Error.
createSession
To retrieve sensitive data on iOS, you'll need to create a session and use its sessionKey for making requests securely. To accomplish this, simply construct your createSession request like this:
BasisTheoryElements.createSession(
apiKey: "<YOUR PUBLIC BT API KEY>"
) { data, error in ... }
The callback provided calls your function with a data of type CreateSessionResponse, and an error of type Error.
getTokenById
This function wraps the get a token API endpoint to retrieve a single strongly typed token. The token's data is transformed to value references which you can use to set the value of your elements without touching the plaintext value and pulling your application into compliance scope.
BasisTheoryElements.getTokenById(
id: "<YOUR TOKEN ID>",
apiKey: "<YOUR API KEY>",
) { data, error in ... }
The callback provided calls your function with a:
errorof type Errordataof typeJSON-JSONis a data structure that has dynamic member lookup capabilities. This allows you to traverse atokenwithout giving you access to read any sensitive values intoken.datawhich means you stay compliant.
To show a value from the token data, traverse the JSON using dot or bracket notation and retrieve the value using the elementValueReference property.
Below is an example of how you do that and set the value reference into a Text Element.
@IBOutlet private weak var myTextElement: TextElementUITextField!
...
BasisTheoryElements.getTokenById(
id: "<YOUR TOKEN ID>",
apiKey: "<YOUR API KEY>",
) { data, error in
myTextElement.setValue(elementValueReference: data!.data!.nested!.property!.elementValueReference)
}
proxy
Proxy provides a simple way to retrieve data back into an element utilizing our proxy service. To accomplish this, simply construct your proxy request like this:
let proxyHttpRequest = ProxyHttpRequest(method: .post, body: [
"testProp": "testValue",
"objProp": [
"nestedTestProp": "nestedTestValue"
]
], headers: [
"X-My-Custom-Header": "headerValue",
])
BasisTheoryElements.proxy(
apiKey: "<YOUR SESSION BT API KEY>",
proxyKey: "<YOUR PROXY KEY>",
proxyHttpRequest: proxyHttpRequest
) { response, data, error in ... }
The callback provided calls your function with a:
responseof type URLResponseerrorof type Errordataof typeJSON-JSONis a data structure that has dynamic member lookup capabilities. This allows you to traverse a response from aproxywithout giving you access to read any sensitiveproxyresponse data, which means you stay compliant. To tokenize a JSON property from aproxyresponse, traverse the JSON using dot or bracket notation and retrieve the value using theelementValueReferenceproperty. As of now, only numbers, booleans, and strings can be retrieved using this method. Below is an example of how you can use a response from aproxywith our elements.
@IBOutlet private weak var myTextElement: TextElementUITextField!
...
BasisTheoryElements.proxy(
apiKey: "<YOUR SESSION BT API KEY>",
proxyKey: "<YOUR PROXY KEY>",
proxyHttpRequest: proxyHttpRequest)
{ response, data, error in
myTextElement.setValue(elementValueReference: data.my?.nested?.property?.elementValueReference)
let body: CreateToken = CreateToken(type: "token", data: [
"myNestedProxyResponseProperty": myTextElement,
])
BasisTheoryElements.createToken(body: body, apiKey: "<YOUR PUBLIC API KEY>")
{ data, error in print(data) }
}
Errors
| Error | Description |
|---|---|
| TokenizingError.applicationTypeNotPublic | The Application API key used is not of type public. Create a public API key through this link. |
| TokenizingError.invalidInput | An element instance used in a tokenization request is invalid. Check the element events on each element to determine which one is invalid. |
| ProxyError.invalidRequest | The proxy request is malformed. Revise the proxy request being attempted. |
| ErrorResponse.error | An instance of ErrorResponse enum gets returned when there's an error from the BasisTheory API. |
ErrorResponse Enum
| Order | Associated Value Name | Description |
|---|---|---|
| 1 | status | An Int describing the response status code |
| 2 | data | A Data? instance describing the response body |
| 3 | urlResponse | The raw UrlResponse? instance |
| 4 | error | The raw Error instance |
ErrorResponse enum can be imported from the BasisTheory Swift SDK through the BasisTheory package, which is a dependency of the iOS BasisTheoryElements package.