All Products
Search
Document Center

:Make API requests

Last Updated:Mar 28, 2024

This topic describes how to call the Fraud Detection API. You can call the Fraud Detection API by using Fraud Detection SDKs or native HTTPS. However, the method of making native HTTPS calls is complex because you must encapsulate the protocol and add and verify a signature. Therefore, we recommend that you use Fraud Detection SDKs to call the Fraud Detection API. This method is simple and convenient.

1. Use Fraud Detection SDKs

When you use Fraud Detection SDKs, you do not need to focus on complex processing such as signature verification and body format construction. We recommend that you use Fraud Detection SDKs. Fraud Detection SDKs for Java, Python, PHP, C#, Golang, Node.js, and Ruby are supported.

1.1. Fraud Detection SDK for Java

JAVA SDK environment preparation, installation please refer to Alibaba Cloud SDK for JAVA.

JAVA Maven dependency:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-saf</artifactId>
    <version>3.0.1</version>
</dependency>

Recommended Java Maven dependency manager:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <optional>true</optional>
    <version>4.5.25</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68.noneautotype</version>
</dependency>
<dependency>        
    <groupId>org.apache.httpcomponents</groupId>        
    <artifactId>httpclient</artifactId>        
    <version>4.5.3</version>    
</dependency>    
<dependency>       
    <groupId>io.opentracing</groupId>       
    <artifactId>opentracing-util</artifactId>        
    <version>0.31.0</version>    
</dependency>

JAVA Use Case:

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
*/
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.saf.model.v20190521.RequestDecisionRequest;
import com.aliyuncs.saf.model.v20190521.RequestDecisionResponse;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
public class RequestDecision {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-x", 
    	"<accessKeyId>", "<accessSecret>");
	//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
        IAcsClient client = new DefaultAcsClient(profile);

        RequestDecisionRequest request = new RequestDecisionRequest();
        request.setEventCode("de_example0123");
        request.setServiceParameters("{\"ip\":\"xx.xx.xx.xx\"}");
        try {
            RequestDecisionResponse response = client.getAcsResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

Output parameters:

{
	"code": 200,
	"data": {
		"tags":"tag1",//Corresponding to Tags on console.
		"score":"100",//Corresponding to Score on console.
    	"finalDecision":"",//Corresponding to Decision on console(PASS,REJECT,PENDING).
        "extend":""//Corresponding to Output variable on console.
	},
	"message": "OK",
	"requestId": "2A226832-83EA-4A44-90B2-6BDDCED5DC72"
}

1.2. Fraud Detection SDK for Python

Python SDK environment preparation, installation please refer to: Alibaba Cloud SDK for Python.

  1. Install the SDK core library.

    • If you are using Python 2.x, run the following command to install the Alibaba Cloud SDK core library:

      pip install aliyun-python-sdk-core

      support version 2.7.16+

      1. request module requests 2.23.0.

      2. Install pyopenssl,since urllib3 needs pyopenssl,pip install pyopenssl ndg-httpsclient pyasn1.

    • If you are using Python 3.x, run the following command to install the Alibaba Cloud SDK core library:

      pip install aliyun-python-sdk-core-v3
  2. Install Alibaba Cloud SAF SDK for Python.

    pip install aliyun-python-sdk-saf==2.0.6

Python use case:

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'ap-southeast-x')
    //SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-
5

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('saf.ap-southeast-x.aliyuncs.com')
//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2019-05-21')
request.set_action_name('RequestDecision')

request.add_query_param('RegionId', "ap-southeast-x")
request.add_query_param('ServiceParameters', "{\"ip\":\"xx.xx.xx.xx\"}")
request.add_query_param('EventCode', "de_example0123")

response = client.do_action(request)
# python2:  print(response) 
print(str(response, encoding = 'utf-8'))

1.3. Fraud Detection SDK for PHP

Download source code:https://github.com/aliyun/aliyun-openapi-php-sdk/tree/master/aliyun-php-sdk-saf

PHP SDK environment preparation, installation please refer to: Alibaba Cloud SDK for PHP.

Test case of Fraud Detection SDK for PHP:

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md

AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('ap-southeast-x')
//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
                        ->asDefaultClient();

try {
    $result = AlibabaCloud::rpc()
                          ->product('saf')
                          // ->scheme('https') // https | http
                          ->version('2019-05-21')
                          ->action('RequestDecision')
                          ->method('POST')
                          ->host('saf.ap-southeast-x.aliyuncs.com')
//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
                          ->options([
                                        'query' => [
                                          'RegionId' => "ap-southeast-1",
                                          'ServiceParameters' => "{\"ip\":\"xx.xx.xx.xx\"}",
                                          'EventCode' => "de_example0123",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}

1.4. Fraud Detection SDK for Golang

Download source code:https://github.com/aliyun/alibaba-cloud-sdk-go/tree/master/services/safGolang SDK environment preparation, installation please refer to: Alibaba Cloud SDK for Golang.Golang use case:

package main

import (
	"fmt"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {
	client, err := sdk.NewClientWithAccessKey("ap-southeast-1", "<accessKeyId>", "<accessSecret>")
	if err != nil {
		panic(err)
	}

	request := requests.NewCommonRequest()
	request.Method = "POST"
	request.Scheme = "https" // https | http
	request.Domain = "saf.ap-southeast-x.aliyuncs.com"
    //SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
	request.Version = "2019-05-21"
	request.ApiName = "RequestDecision"
  request.QueryParams["RegionId"] = "ap-southeast-x"
    //SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
  request.QueryParams["ServiceParameters"] = "{\"ip\":\"xx.xx.xx.xx\"}"
  request.QueryParams["EventCode"] = "de_example0123"
	
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}
	fmt.Print(response.GetHttpContentString())
}

1.5. Fraud Detection SDK for Node.js

Download source code:https://github.com/aliyun/openapi-core-nodejs-sdkNode.js SDK environment preparation, installation please refer to: Alibaba Cloud SDK for Node.js.

Execute the following instruction to insatall @alicloud/pop-core. The "--save"in instruction will write this module in package.jason as dependency.

$ npm install @alicloud/pop-core --save

Node.js use case:

const Core = require('@alicloud/pop-core');

var client = new Core({
  accessKeyId: '<accessKeyId>',
  accessKeySecret: '<accessSecret>',
  endpoint: 'https://saf.ap-southeast-x.aliyuncs.com',
//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
  apiVersion: '2019-05-21'
});

var params = {
  "RegionId": "ap-southeast-x",
//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
    "ServiceParameters": "{\"ip\":\"xx.xx.xx.xx\"}",
    "EventCode": "de_example0123"
}

var requestOption = {
  method: 'POST'
};

client.request('RequestDecision', params, requestOption).then((result) => {
  console.log(JSON.stringify(result));
}, (ex) => {
  console.log(ex);
})