Skip to content

Script Development / API Authentication

For HTTP APIs generated by "Func API", additional interface authentication can be added.

Currently supported interface authentications are as follows:

Authentication Type Description
Fixed Field Verify that the request's Header, Query, or Body must contain a field with a specific value
HTTP Basic Standard HTTP Basic authentication (a login box will pop up when accessed in a browser)
HTTP Digest Standard HTTP Digest authentication (a login box will pop up when accessed in a browser)
Authentication Function Specify a self-written function as the authentication function

Users can add authentication configurations in "Manage / API Authentication", and then specify the added authentication configuration in "Func API Configuration".

If high security is required, be sure to access the interface using HTTPS

1. Fixed Field Authentication

Fixed Field Authentication is the simplest authentication method, where the client and DataFlux Func agree to include a specific field and field value somewhere in the request (Header, Query, or Body), and attach this content with each call to complete authentication.

Assuming it is agreed that each request must include x-auth-token="my-auth-token" in the header, then the following call will complete the authentication:

Text Only
1
2
GET /api/v1/al/func-api-xxxxx
x-auth-token: my-auth-token

When configuring multiple fixed field authentications, a single match is considered as passing the authentication

For fields used for authentication in Query and Body, the system will automatically delete them after authentication is passed, and they will not be passed to the function

2. HTTP Basic / HTTP Digest

Authentication methods directly supported by browsers.

Interfaces using this authentication method will prompt the browser to pop up a username/password box when accessed directly in the browser's address bar.

If you need to access it programmatically, please refer to the following code:

Python
1
2
3
4
5
6
7
8
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth

# HTTP Basic Authentication
resp = requests.get(url_1, auth=HTTPBasicAuth('user', 'password'))

# HTTP Digest Authentication
resp = requests.get(url_2, auth=HTTPDigestAuth('user', 'password'))

3. Authentication Function

If the interface authentication method is complex or special (such as needing to connect to a business system, etc.), you can choose to write your own function for authentication.

The function used for authentication does not require parameters, and returning True indicates successful authentication, returning other content or throwing an error indicates failure.

In the authentication function, you can use the built-in variable _DFF_HTTP_REQUEST to get request-related information: Script Development / Built-in Features / Built-in Variables / _DFF_HTTP_REQUEST

Example
1
2
3
@DFF.API('Authentication Function')
def my_auth_func():
    return _DFF_HTTP_REQUEST['headers']['x-auth-token'] == 'my-auth-token'

It should be noted that when authentication fails, the response format of the interface will vary depending on the returned content:

Example
1
2
3
@DFF.API('Authentication Function')
def my_auth_func():
    return False

Authentication failed, when directly returning False, the response body will not contain any specific error information:

Interface Response Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
  "ok"     : false,
  "error"  : 401.99,
  "reason" : "EAPIAuth",
  "message": "Func Auth failed",
  "detail" : false,
  "status" : 401,
  "reqDump": {
    "method": "GET",
    "url"   : "http://localdev:8089/api/v1/func-api/xxxxx/s"
  },
  "traceId"   : "TRACE-XXXXX",
  "clientTime": null,
  "reqTime"   : "2025-08-14T11:03:45.238Z",
  "respTime"  : "2025-08-14T11:03:45.406Z",
  "reqCost"   : 168
}
Example
1
2
3
@DFF.API('Authentication Function')
def my_auth_func():
    return 'Bad User!'

Authentication failed, when returning a string, this string will be returned as the detail field:

Interface Response Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
  "ok"     : false,
  "error"  : 401.99,
  "reason" : "EAPIAuth",
  "message": "Func Auth failed",
  "detail" : "Bad User!",
  "status" : 401,
  "reqDump": {
    "method": "GET",
    "url"   : "http://localdev:8089/api/v1/func-api/xxxxx/s"
  },
  "traceId"   : "TRACE-XXXXX",
  "clientTime": null,
  "reqTime"   : "2025-08-14T11:03:45.238Z",
  "respTime"  : "2025-08-14T11:03:45.406Z",
  "reqCost"   : 168
}
Example
1
2
3
@DFF.API('Authentication Function')
def my_auth_func():
    return { 'error': 'Bad User!' }

Authentication failed, when returning JSON, this JSON will be returned as the detail field:

Interface Response Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "ok"     : false,
  "error"  : 401.99,
  "reason" : "EAPIAuth",
  "message": "Func Auth failed",
  "detail": {
    "error": "Bad User!"
  },
  "status": 401,
  "reqDump": {
    "method": "GET",
    "url"   : "http://localdev:8089/api/v1/func-api/xxxxx/s"
  },
  "traceId"   : "TRACE-XXXXX",
  "clientTime": null,
  "reqTime"   : "2025-08-14T11:03:45.238Z",
  "respTime"  : "2025-08-14T11:03:45.406Z",
  "reqCost"   : 168
}
Example
1
2
3
@DFF.API('Authentication Function')
def my_auth_func():
    raise Exception('Bad User!')

When the authentication function throws an error, since the Func framework cannot perceive whether this Exception belongs to business logic or the code itself, it will return generic error information as the detail field:

Interface Response Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
  "ok": false,
  "error": 401.99,
  "reason": "EAPIAuth",
  "message": "Func Auth failed",
  "detail": {
    "name"               : "Func.Runner",
    "id"                 : "task-RWLH3EuCRfYl",
    "triggerTime"        : 1755170213.119,
    "startTime"          : 1755170213.122,
    "endTime"            : 1755170213.13,
    "status"             : "failure",
    "exceptionType"      : "UserScriptException",
    "exception"          : "In User Script: Exception('Bad User!')",
    "exceptionFrom"      : "userScript",
    "originExceptionType": "Exception",
    "originException"    : "Exception('Bad User!')"
  },
  "status": 401,
  "reqDump": {
    "method": "GET",
    "url"   : "http://localdev:8089/api/v1/func-api/xxxxx/s"
  },
  "traceId"   : "TRACE-XXXXX",
  "clientTime": null,
  "reqTime"   : "2025-08-14T11:03:45.238Z",
  "respTime"  : "2025-08-14T11:03:45.406Z",
  "reqCost"   : 168
}