Updated on 2025-07-10 GMT+08:00

(Advanced) Accessing Applications Using APIs

API access is disabled by default for the Stable Diffusion application deployed in the application center. You can enable API access by Configuring Function Environment Variables.

Accessing the Application Using an API

  1. Go to the Stable Diffusion application details page, find the function resource whose logic name is stable_diffusion in the Resource tab, and click the link to go to the function details page.
  2. Choose Configuration > Environment Variables, click Edit Environment Variable. In the dialog box that is displayed, click Add, add the information in the following table, and click OK.

    Table 1 Environment variables for accessing applications using APIs

    Key

    Value

    Description

    EXTRA_ARGS

    --api --api-auth username1:password1,username2:password2 --nowebui

    Enter the usernames in username1 and username2, and enter the passwords in password1 and password2. Use commas (,) to separate the usernames and passwords of multiple users.

  3. After the configuration is complete, you need to enter the username and password when accessing the application using APIs.

Configuring Concurrency Parameters

Configure concurrency parameters by referring to Configuring Concurrency Parameters. The recommended parameters in WebUI and API modes are as follows:
  • WebUI mode
    • Set the value of Max. Requests per Instance to 100 or higher. According to testing results, in single-user scenarios, you are advised to set the Max. Requests per Instance to around 15. For multi-user scenarios, the value should be configured to 100 or higher.
    • Set the value of Max. Instances per Function to 1. In WebUI mode, the task progress is continuously monitored during image generation. If there are multiple instances, requests may be disordered, which may cause obstacles in progress display and final result display.
  • API mode
    • Set the value of Max. Requests per Instance between 1 and 5 to ensure that a single instance does not process too many requests. When the concurrent requests reach the upper limit, new instances are triggered to ensure image generation efficiency.
    • Set the value of Max. Instances per Function to 400 (default). The value can be changed as needed.

Accessing an Application Using APIs and Enabling WebUI Authentication

To access an application using APIs and enable WebUI authentication, you can set environment variables by referring to Table 2.

Table 2 Accessing an application using APIs and enabling WebUI authentication

Key

Value

Description

EXTRA_ARGS

--api --api-auth user1:password1 --gradio-auth user1:password1

Enter the usernames in username1 and username2, and enter the passwords in password1 and password2. Use commas (,) to separate the usernames and passwords of multiple users.