All Products
Search
Document Center

Identity as a Service:Identity as a Service:Access Model Studio AI services without static keys

Last Updated:Mar 31, 2026

Use the Machine-to-Machine (M2M) authentication feature of Alibaba Cloud Identity as a Service (IDaaS) EIAM together with AI Gateway to let your AI applications call Alibaba Cloud Model Studio large language model (LLM) APIs using short-lived access tokens instead of static API keys.

Why avoid static API keys

Traditional AI applications embed a static API key in code or configuration files to call LLM APIs. This approach creates security risks:

  • A leaked API key grants permanent access until you manually revoke it.

  • Static keys often carry broader permissions than any single application needs.

  • Key rotation is manual and error-prone.

The solution: AI Gateway supports JWT-formatted tokens. Instead of storing a plaintext key, your application requests a short-lived access token from IDaaS EIAM using the OAuth 2.0 client credentials grant. AI Gateway validates the token, then calls Model Studio on your behalf. Static credential management is centralized at the gateway layer, and no secret ever appears in your application code.

How it works

The following diagram shows the runtime request flow:

Architecture diagram
  1. Your application sends a token request to the IDaaS EIAM token endpoint using its client_id and client_secret.

  2. IDaaS EIAM validates the credentials and returns a short-lived JWT access token.

  3. Your application includes the access token as a Bearer token in the Authorization header and calls AI Gateway.

  4. AI Gateway validates the JWT against the IDaaS EIAM JWKS endpoint locally, without calling IDaaS for every request.

  5. After authorization succeeds, AI Gateway uses the pre-configured Model Studio API key to call the backend LLM service and returns the result to your application.

Prerequisites

Before you begin, make sure you have:

  • An Alibaba Cloud account with access to IDaaS EIAM, AI Gateway, and Model Studio

  • A Model Studio API key (sk-xxxx) for the model you want to use

  • An existing IDaaS EIAM instance

Step 1: Get a Model Studio API key

  1. Go to the Key Management page of Alibaba Cloud Model Studio (Singapore | Beijing).(Singapore| Beijing)

  2. Copy an existing API key from the list, or click Create API Key to create one.

  3. In the Create API Key dialog, set Owner Account to Alibaba Cloud account and Owner Workspace to Default Workspace, then click OK.

  4. Click the copy icon next to the newly created key to copy it.

For more information, see Obtain an API key.

Step 2: Set up AI Gateway to proxy Model Studio

Create an Internet NAT gateway

AI Gateway runs in a Virtual Private Cloud (VPC) and needs outbound internet access to reach Model Studio. Create an Internet NAT gateway in the same VPC and region you will use for AI Gateway.

  1. Go to the Internet NAT Gateway purchase page, configure the following settings, and click Buy Now.

    Configuration itemDescriptionExample value
    Billing methodOnly Pay-as-you-go is supported.Pay-as-you-go
    RegionRegion where the NAT gateway will be created.China (Hangzhou)
    Network and zoneVPC and vSwitch for the NAT gateway. This cannot be changed after creation.TEST_VPC
    Elastic IP addressSelect an existing elastic IP address (EIP) or purchase a new one.Purchase new EIP
  2. In the VPC console, go to NAT Gateway > Internet NAT gateway and locate the instance you created.

  3. Click the instance ID to open the details page.

  4. On the SNAT management tab, click Create SNAT entry.

  5. Set SNAT entry granularity to VPC, set Elastic IP address to the associated EIP, and click OK.

For more information, see Create an Internet NAT gateway and associate an EIP.

Create an AI Gateway instance

  1. Go to the AI Gateway purchase page, configure the following settings, and click Buy Now.

    The following settings create a public-network instance with minimum specifications. Keep the default values for any setting not listed here. For more information, see Create a gateway instance.
    Configuration itemDescriptionExample value
    Product typeBilling type. Supports Dedicated Instance (Pay-as-you-go), Dedicated Instance (Subscription), and Serverless (Pay-as-you-go).Dedicated Instance (Pay-as-you-go)
    RegionMust match the region of the NAT gateway you created.China (Hangzhou)
    Gateway nameCustom name for the gateway.test_gateway
    Gateway specificationsNode specifications. Not applicable to Serverless instances.aigw.small.x1
    Network access typeSupports Public Network, Private Network, and Public and Private Network.Public Network
    VPCMust match the VPC of the NAT gateway you created.TEST_VPC
    Zone selectionAutomatic assignment or Manual selection.Automatic assignment
    vSwitchvSwitch for the gateway nodes.VSW_1
    Service-linked RoleClick all Create Service-linked Role buttons to create the following two roles. For more information, see Service-linked role. <br>- AliyunServiceRoleForNativeApiGw<br>- AliyunServiceRoleForNativeApiGwInvokeFC-
  2. On the Confirm order page, review the AI Gateway configuration details and click Buy Now.

  3. Return to the AI Gateway Instances page. Wait for the instance status to change to Running (this takes 1 to 5 minutes).

    Note

    The public endpoint provided by AI Gateway has a daily call limit of 1,000 requests and is for testing only. For production environments, resolve your business domain name to this public endpoint using a CNAME record and use the custom domain for all production calls. The public endpoint is listed under Overview > Endpoints > Endpoints and IPs on the instance details page.

Create an AI service backend

  1. In the AI Gateway console, go to AI Gateway > Instances and click the target instance ID.

  2. In the left navigation pane, click Services, then click Create service.

  3. Configure the following settings and click OK.

    The following settings use Alibaba Cloud Model Studio as an example. For more information, see Create a service.
    Configuration itemDescriptionExample value
    Service sourceType of service to proxy.AI service
    Service nameCustom name for the service.test_bailian
    Large model providerThe LLM provider to proxy.Alibaba Cloud Model Studio
    Model protocolKeep the default value.OpenAI/v1
    Endpoint (base_url)The endpoint provided by the model provider. Keep the default value.https://dashscope.aliyuncs.com/compatible-mode/v1
    Generation methodHow to provide the API key.Manual input
    API-KEYThe Model Studio API key you obtained in Step 1.sk-4901\*\*\*\*\*\*8a59

Create a Model API

  1. On the instance details page, go to Model API in the left navigation pane and click Create Model API.

  2. Select the Text generation scenario and click Create. Configure the following settings and click OK.

    For more information, see Manage Model APIs.
    Configuration itemDescriptionExample value
    ProtocolDetermines the built-in route schema. Use this to generate compatible interfaces such as OpenAI, DashScope, and vLLM.OpenAI compatible
    API nameMust be globally unique within your account.test_api
    Domain nameThe domain name used to access this API. The combination of domain name and base path must be unique.example.aliyun.com
    Service typeRouting method for backend services.Single model service
    Service nameThe backend service to route to.test_bailian
  3. On the API details page, click the Consumer authentication tab. Click Configuration information > Edit, turn on the Enable switch, set Authentication method to JWT, and click OK.

Step 3: Configure an IDaaS M2M application

You need two M2M applications in IDaaS EIAM: an M2M Server application (the OAuth 2.0 resource server, representing AI Gateway) and an M2M Client application (the OAuth 2.0 client, representing your AI application).

Create an IDaaS instance

If you do not have an IDaaS EIAM instance, create one first:

  1. In the IDaaS Management Console, go to EIAM.

  2. Click IDaaS > Create instance. Set a Description, accept the Alibaba Cloud Product Service Agreement, and click Create.

  3. In the EIAM instance list, click Console in the Actions column to open the instance management console.

  4. Go to Application management > M2M application. In the Note on upgrade dialog, click Upgrade now.

  5. On the upgrade page, enable M2M management and click Buy Now.

Create an M2M Server application

The M2M Server application acts as the OAuth 2.0 resource server. It defines the permission scopes that clients can request and provides the JWKS endpoint for JWT verification.

  1. In the EIAM instance management console, go to Application management > M2M application.

  2. Click Add application, set Application name to "M2M Server", and click Add.

  3. On the Server permission control tab, under Authorized application, turn on the Server permission control switch. Set the ResourceServer identifier to the following value, then click Confirm.

    cloud:idaas:aigateway:alibabacloud:<account-id>

    Replace <account-id> with your Alibaba Cloud account ID.

  4. Under Permission management, click Create scope. Set the Scope name and Scope value, then click Confirm.

  5. Turn on the M2M Client switch under Authorized Applications. In the Permission list, select the permission you added in the previous step and click Confirm.

  6. On the General tab, copy and save the Public key endpoint URL from Application settings. You will use this URL when configuring AI Gateway JWT authentication in Step 4.

Create an M2M Client application

The M2M Client application represents your AI application and holds the credentials it uses to request access tokens.

  1. In the EIAM instance management console, go to Application management > M2M application.

  2. Click Add application, set Application name to "M2M Client", and click Add.

  3. On the General tab, click Certificate management > Client secret credential > Add client_secret. Select a Client secret validity period and click Confirm.

  4. Click View next to client_secret. Copy and save the following values for later use:

    • client_id (example: app_m5doo******)

    • client_secret (example: CS5v3F******)

  5. Copy and save the Token endpoint URL from Application settings (example: https://******.aliyunidaas.com/api/v2/iauths_system/oauth2/token).

  6. On the Client permission management tab, turn on the Custom permissions switch.

Step 4: Configure AI Gateway JWT authentication

Create a consumer in AI Gateway that uses JWT authentication. The consumer tells AI Gateway how to validate tokens issued by IDaaS EIAM.

Create a consumer and configure JWT authentication

  1. In the AI Gateway console, go to AI Gateway > Consumers and click Create consumer.

  2. Configure the following settings and click Create.

    For more information, see Create a consumer.
    Configuration itemDescriptionExample value
    Consumer nameCustom name for the consumer.test_consumer
    StatusConsumer status. You can change this after creation.Enabled
    Authentication methodSet to JWT.JWT
    Creation methodHow AI Gateway fetches the JWKS.Remote fetch
    JWKS URLThe public key endpoint URL from the M2M Server application (Step 3).URL from Public key endpoint
    Consumer ID in JWKS payload > keyThe JWT claim used as the consumer identifier.scope
    Consumer ID in JWKS payload > ValueThe scope value to match.Scope value from Step 3

Grant API permissions to the consumer

  1. On the Consumers list page, click the consumer name to open its details page, then click the Consumer authorization tab.

  2. Click the Model API tab, then click Grant.

  3. In the authorization panel, select the Model API you created in Step 2 and click OK.

For more information, see Authorization management.

Step 5: Verify the configuration

Get an M2M access token

Important

This example uses the client_secret_post authentication method. For information about other authentication methods, including OIDC federation, PKCS#7 signature authentication, and PrivateCA certificate authentication, see M2M Client Token call examples.

Run the following command to request an access token from IDaaS EIAM:

curl --location --request POST '<Token endpoint. Example: https://******.aliyunidaas.com/api/v2/iauths_system/oauth2/token>' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=client_credentials' \
  --data-urlencode 'client_id=<client_id. Example: app_m5doo******>' \
  --data-urlencode 'client_secret=<client_secret. Example: CS5v3F******>' \
  --data-urlencode 'scope=<ResourceServer Identifier. Example: cloud:idaas:aigateway:alibabacloud:******>|<Scope value>'

Replace the placeholders with the values you saved in Step 3:

  • <Token endpoint>: The Token endpoint URL from the M2M Client application.

  • <client_id>: The client_id from the M2M Client application.

  • <client_secret>: The client_secret from the M2M Client application.

  • <ResourceServer Identifier>: The ResourceServer identifier from the M2M Server application.

  • <Scope value>: The Scope value from the M2M Server application.

The scope parameter must include both the ResourceServer identifier and the scope value, separated by a vertical bar (|).

A successful response looks like:

{
  "token_type": "Bearer",
  "access_token": "eyJraWQi******Nsn2u7Jcw",
  "expires_in": 3600,
  "expires_at": 1765448721
}

Copy the access_token value. It expires after expires_in seconds (typically 3,600 seconds).

Call the LLM API through AI Gateway

Use the access token as a Bearer token to send a request to AI Gateway:

curl --location '<Domain name. Example: http://env-d4t6******kv0-cn-hangzhou.alicloudapi.com>/v1/chat/completions' \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <M2M Token>' \
  --data '{
    "model": "qwen-max",
    "stream": true,
    "max_tokens": 1024,
    "top_p": 0.95,
    "temperature": 1,
    "messages": [
      {
        "role": "user",
        "content": "<prompt>"
      }
    ]
  }'
Important

There must be exactly one space between Bearer and the access token. Replace <Domain name> with the public endpoint from Overview > Endpoints > Endpoints and IPs on the AI Gateway instance details page.

A streaming response from Model Studio confirms that the end-to-end integration is working.

Debug using the AI Gateway console

To verify the configuration using the built-in debug tool:

  1. In the AI Gateway console, go to AI Gateway > Instances and click the target instance ID.

  2. In the left navigation pane, click Model API, then click the Model API name.

  3. In the upper-right corner, click Debug.

  4. Configure the following parameters:

    • Domain name: Select a public endpoint, such as env-d4t******kv0-cn-hangzhou.alicloudapi.com.

    • Under Header, click Add parameter and add Authorization with the value Bearer <access_token>.

    • Prompt: Enter a prompt to send to the LLM.

  5. Click Send request and view the result in the Model response section.

End-to-end Python example

The following example combines token acquisition and the API call in a single script:

import os
import requests
from openai import OpenAI

# Step 1: Get an M2M access token from IDaaS EIAM
TOKEN_ENDPOINT = os.environ.get("IDAAS_TOKEN_ENDPOINT")
CLIENT_ID = os.environ.get("IDAAS_CLIENT_ID")
CLIENT_SECRET = os.environ.get("IDAAS_CLIENT_SECRET")
SCOPE = os.environ.get("IDAAS_SCOPE")  # Format: <ResourceServer Identifier>|<Scope value>

token_response = requests.post(
    TOKEN_ENDPOINT,
    headers={"Content-Type": "application/x-www-form-urlencoded"},
    data={
        "grant_type": "client_credentials",
        "client_id": CLIENT_ID,
        "client_secret": CLIENT_SECRET,
        "scope": SCOPE,
    },
)
token_response.raise_for_status()
access_token = token_response.json()["access_token"]

# Step 2: Call Model Studio through AI Gateway using the access token
AI_GATEWAY_BASE_URL = os.environ.get("AI_GATEWAY_BASE_URL")  # Example: http://env-d4t6******kv0-cn-hangzhou.alicloudapi.com/v1

client = OpenAI(
    base_url=AI_GATEWAY_BASE_URL,
    api_key="unset",
    default_headers={"Authorization": f"Bearer {access_token}"},
)

stream = client.chat.completions.create(
    model="qwen-max",
    messages=[{"role": "user", "content": "Hello, how are you?"}],
    stream=True,
    max_tokens=1024,
    top_p=0.95,
    temperature=1,
)

for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Set the following environment variables before running the script:

Environment variableDescription
IDAAS_TOKEN_ENDPOINTToken endpoint URL from the M2M Client application
IDAAS_CLIENT_IDclient_id from the M2M Client application
IDAAS_CLIENT_SECRETclient_secret from the M2M Client application
IDAAS_SCOPE`<ResourceServer Identifier><Scope value>`
AI_GATEWAY_BASE_URLAI Gateway public endpoint with /v1 suffix

Production checklist

RecommendationDetails
Use federated authenticationFor production, replace client_secret_post with a federated credential method such as OIDC federation, PKCS#7 signature authentication, or PrivateCA certificate authentication.
Cache access tokensReuse the access token within its validity period (expires_in seconds) instead of requesting a new one for every API call. This reduces latency and IDaaS load.
Use a custom domainThe AI Gateway public endpoint has a daily call limit of 1,000 requests. Resolve your business domain name to the public endpoint using a CNAME record and use the custom domain in production.
Apply least privilegeGrant only the minimum scope required to the M2M Client application. Do not share a single client credential across multiple applications with different permission needs.
Rotate client secretsRotate the client_secret of your M2M Client application regularly and update all services that use it.
Monitor token usageTrack access token issuance and API call volume in IDaaS and AI Gateway to detect anomalous behavior early.