For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Notifying HUMAN’s backend on outgoing URL requests
Setting additional data (optional)
How to Start the SDK
Initialize Early: Start the SDK as soon as possible in your app’s lifecycle to ensure all URL requests include the SDK’s HTTP headers. Delaying SDK initialization may result in requests being blocked by HUMAN’s Enforcer.
Android: Initialize in the Application’s onCreate method.
iOS: Initialize in the AppDelegate’s didFinishLaunchingWithOptions method.
Important: Don’t forget to replace <APP_ID> with your actual AppID.
Explanation of the Code
Initialization: Start the SDK as early as possible on the main thread to ensure all URL requests include the necessary HTTP headers.
Policy Configuration: Create an HSPolicy instance to configure the SDK’s behavior. Set the automaticInterceptorPolicy.interceptorType to HSAutomaticInterceptorType.NONE to disable automatic interception of URL requests.
Starting the SDK: Call the HumanSecurity.start(appId:policy:) function with the application instance (Android only), your AppID, and the configured policy.
Notes:
iOS: The Automatic Interception uses URLSessionConfiguration.default. If your app uses a custom configuration, set it in the policy using HSAutomaticInterceptorPolicy.urlSessionConfiguration.
Multiple AppIDs: If your app communicates with multiple servers having different AppIDs, use the HumanSecurity.start(appIds:policy:) function to pass an array of AppIDs and specify the relevant AppID for each API call.
Bot Defender Integration
How to Add SDK’s HTTP Headers to Your URL Requests and Handle Blocked Requests
Automatic Header Addition: The SDK provides HTTP headers that should be added to your app’s URL requests. It is essential that these HTTP headers are included in every URL request.
No Caching of Headers: Do not cache these HTTP headers as they contain a token with an expiration date. The SDK manages this token to ensure it is always up-to-date.
Automatic Handling of Blocked Requests: The SDK handles blocked requests and presents a challenge to the user automatically.
Example Implementation
Android
In this example, we assume your app uses OkHttp or Ktor. We recommend creating a custom Interceptor for this task. However, you may use any HTTP client of your choice and implement the same logic.
NSLog(@"Blocked response was handled by the SDK");
24
}
25
}
26
}];
27
[dataTask resume];
28
}
29
30
@end
Using Alamofire:
Swift:
1
import Alamofire
2
import HUMAN
3
4
class MyHttpClient {
5
6
func sendUrlRequest(url: URL) {
7
var myHttpHeaders = [String: String]()
8
9
// Configure your HTTP headers...
10
11
let humanHttpHeaders = HumanSecurity.BD.headersForURLRequest(forAppId: "<APP_ID>")
12
let allHTTPHeaderFields = myHttpHeaders.merging(humanHttpHeaders) { $1 }
13
14
AF.request(url, headers: HTTPHeaders(allHTTPHeaderFields)).response { response in
15
if let data = response.data {
16
let isHandled = HumanSecurity.BD.handleBlockResponse(data: data, url: url.absoluteString) { result in
17
print("Challenge result = \(result)")
18
}
19
if isHandled {
20
print("Blocked response was handled by the SDK")
21
}
22
}
23
}
24
}
25
}
Explanation of the Code
Adding HTTP Headers: Retrieve the SDK’s HTTP headers using HumanSecurity.BD.headersForURLRequest("<APP_ID>") and add them to your URL requests.
Sending the Request: Execute the URL request using your configured HTTP client.
Handling the Response: Use the SDK to determine if the received error indicates a “blocked request.” If so, the SDK presents a challenge to the user.
Note: While your request handler is called, the SDK presents a challenge to the user.
What to Do When a Request Is Blocked
Handle as a Failure: Treat blocked requests as failures. Ensure that your app’s UI reflects that the action can be retried after the challenge is solved or canceled by the user. If the request was triggered by a user’s action, inform the user that they may retry the same action.
Use Handler Callback for Analytics: Utilize the handler callback to log analytics, create logs, etc.
Optional Request Retry: You may use the handler callback to retry the original request when appropriate. Consider the following:
Callback Scope: The callback is called outside the original request’s scope. Ensure your app handles this correctly.
Challenge Cancellation: If the challenge is canceled by the user, do not retry the request to avoid it being blocked again.
Retry Block Possibility: The retry attempt may also be blocked. Do not assume it will succeed.
Understanding the Block Response
Enforcer Decision: When HUMAN’s Enforcer decides to block a request, it returns a JSON string in the response body with an HTTP status code of 403. Example response:
Passing Response to SDK: Your app should pass the entire JSON response to the SDK via the HSBotDefender/handleBlockResponse function. If not, the SDK won’t present a challenge to the user.
Setting Custom Parameters for Bot Defender (Optional)
You can configure HUMAN’s backend with additional parameters by setting custom parameters.
Android: Use a HashMap<String, String>.
iOS: Use a Dictionary<String, String>.
Key Format: Use keys in the format custom_param[x], where [x] is a number between 1-10.
Important: Call the HSBotDefender.setCustomParameters(parameters:forAppId:) function only after the HumanSecurity.start(appId:policy:) function has been called.
Example Implementation
Android
Kotlin:
1
import com.humansecurity.mobile_sdk.HumanSecurity
2
3
fun setCustomParametersForBotDefender() {
4
try {
5
val customParameters = HashMap<String, String>().apply {
How to Notify HUMAN’s Backend on Outgoing URL Requests from the App
To enable Account Defender to protect the user’s account, your app must provide the SDK with outgoing URL requests.
Android
In this example, we assume your app uses OkHttp or Ktor. We recommend creating a custom Interceptor for this task. However, you may use any HTTP client of your choice and implement the same logic.
Kotlin:
1
import com.humansecurity.mobile_sdk.HumanSecurity
2
import okhttp3.Interceptor
3
import okhttp3.Response
4
5
class AccountDefenderInterceptor : Interceptor {
6
7
override fun intercept(chain: Interceptor.Chain): Response {
Setting Additional Data for Account Defender (Optional)
You can configure HUMAN’s backend with additional parameters by setting additional data.
Android: Use a HashMap<String, String>.
iOS: Use a Dictionary<String, String>.
Important: Call the HSAccountDefender.setAdditionalData(parameters:forAppId:) function only after the HumanSecurity.start(appId:policy:) function has been called.
Example Implementation
Android
Kotlin:
1
import com.humansecurity.mobile_sdk.HumanSecurity
2
3
fun setAdditionalDataForAccountDefender() {
4
try {
5
val additionalData = HashMap<String, String>().apply {