NuwaAI Digital Human PlatformNuwaAI Digital Human Platform
  • 简体中文
  • English
  • 简体中文
  • English
  • Authentication

Authentication

1. Overview

Authentication is used to obtain and manage access tokens for protected API endpoints.

2. Authentication

2.1 Get an API key

Get an API key

2.2 Generate an access token with an API key

Request

  • Method: POST
  • Path: https://api.nuwaai.com/web/apiKey/auth
  • Body:
{
  "secretKey": "{apikey}"
}

Parameters

NameTypeRequiredDescription
apikeyStringYesKey generated from the API key

Response

{
  "code": {code},
  "msg": {message},
  "data": {data}
}
NameTypeRequiredDescription
codeintYesResponse code: 0 means OK; other values indicate an error.
messageStringNoError details when the response code is not 0.
dataStringYesThe returned access token when the response code is 0.
示例
const secretKey = 'sk-jqZ-gBPcRCG1nLpaenmS1gew7rcCkIs-NUWSPzpd3DeWYxobM_CHm-uPTCa8gDPv';

fetch('https://api.nuwaai.com/web/apiKey/auth', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    secretKey: secretKey,
  }),
})
  .then(res => res.json())
  .then(data => {
    if (data.code === 0) {
      console.log('token:', data.data);
      localStorage.setItem('token', data.data);
    } else {
      console.error('Request failed:', data.msg);
    }
  })
  .catch(error => {
    console.error('Request error:', error);
  });
import Foundation

func getAccessToken() {
    let secretKey = "sk-jqZ-gBPcRCG1nLpaenmS1gew7rcCkIs-NUWSPzpd3DeWYxobM_CHm-uPTCa8gDPv"
    let url = URL(string: "https://api.nuwaai.com/web/apiKey/auth")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    
    let body: [String: String] = ["secretKey": secretKey]
    request.httpBody = try? JSONSerialization.data(withJSONObject: body)
    
    URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data else { return }
        do {
            if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
               let code = json["code"] as? Int,
               code == 0,
               let token = json["data"] as? String {
                print("token: \(token)")
                // Store the token
                UserDefaults.standard.set(token, forKey: "nuwaAccessToken")
            }
        } catch {
            print("Error: \(error)")
        }
    }.resume()
}
import okhttp3.*
import org.json.JSONObject

fun getAccessToken() {
    val secretKey = "sk-jqZ-gBPcRCG1nLpaenmS1gew7rcCkIs-NUWSPzpd3DeWYxobM_CHm-uPTCa8gDPv"
    val client = OkHttpClient()
    val url = "https://api.nuwaai.com/web/apiKey/auth"
    
    val body = JSONObject()
    body.put("secretKey", secretKey)
    
    val request = Request.Builder()
        .url(url)
        .post(RequestBody.create(MediaType.parse("application/json"), body.toString()))
        .build()
    
    client.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call, e: IOException) {
            e.printStackTrace()
        }
        
        override fun onResponse(call: Call, response: Response) {
            response.body()?.let {
                val json = JSONObject(it.string())
                if (json.getInt("code") == 0) {
                    val token = json.getString("data")
                    println("token: $token")
                    // Store the token
                    val sharedPreferences = getSharedPreferences("nuwa", Context.MODE_PRIVATE)
                    sharedPreferences.edit().putString("accessToken", token).apply()
                }
            }
        }
    })
}
const secretKey = 'sk-jqZ-gBPcRCG1nLpaenmS1gew7rcCkIs-NUWSPzpd3DeWYxobM_CHm-uPTCa8gDPv';

wx.request({
  url: 'https://api.nuwaai.com/web/apiKey/auth',
  method: 'POST',
  header: {
    'Content-Type': 'application/json',
  },
  data: {
    secretKey: secretKey,
  },
  success: function(res) {
    if (res.data.code === 0) {
      console.log('token:', res.data.data);
      wx.setStorageSync('token', res.data.data);
    } else {
      console.error('Request failed:', res.data.msg);
    }
  },
  fail: function(error) {
    console.error('Request error:', error);
  }
});
Last Updated:: 9/4/26, 4:43 PM
Contributors: 邹丹晔, 005609