create method

Future<ApiKey> create({
  1. String? name,
  2. int? expiresIn,
  3. String? userId,
  4. String? prefix,
  5. int? remaining,
  6. Map<String, dynamic>? metadata,
  7. int? refillAmount,
  8. int? refillInterval,
  9. int? rateLimitTimeWindow,
  10. int? rateLimitMax,
  11. bool? rateLimitEnabled,
})

Create an API Key

name The name of the API Key expiresIn The expiration time of the API Key, in seconds userId The user ID of the user that the API Key belongs to. Useful for server-side only. prefix The prefix of the API Key remaining The remaining number of requests. Server side only metadata The metadata of the API Key refillAmount The amount to refill the remaining count of the API Key. Server Only Property refillInterval The interval to refill the remaining count of the API Key. Server Only Property. rateLimitTimeWindow The duration in milliseconds where each request is counted. Once the maxRequests is reached, the request will be rejected until the timeWindow has passed, at which point the timeWindow will be reset. Server Only Property. rateLimitMax The maximum amount of requests allowed within a window. Once the maxRequests is reached, the request will be rejected until the timeWindow has passed, at which point the timeWindow will be reset. Server Only Property rateLimitEnabled Whether the key has rate limiting enabled. Server Only Property.

Implementation

Future<ApiKey> create({
  String? name,

  int? expiresIn,

  String? userId,

  String? prefix,

  int? remaining,

  Map<String, dynamic>? metadata,

  int? refillAmount,

  int? refillInterval,

  int? rateLimitTimeWindow,

  int? rateLimitMax,

  bool? rateLimitEnabled,
}) async {
  try {
    final body = {};
    if (name != null) body['name'] = name;
    if (expiresIn != null) body['expiresIn'] = expiresIn;
    if (userId != null) body['userId'] = userId;
    if (prefix != null) body['prefix'] = prefix;
    if (remaining != null) body['remaining'] = remaining;
    if (metadata != null) body['metadata'] = metadata;
    if (refillAmount != null) body['refillAmount'] = refillAmount;
    if (refillInterval != null) body['refillInterval'] = refillInterval;
    if (rateLimitTimeWindow != null) body['rateLimitTimeWindow'] = rateLimitTimeWindow;
    if (rateLimitMax != null) body['rateLimitMax'] = rateLimitMax;
    if (rateLimitEnabled != null) body['rateLimitEnabled'] = rateLimitEnabled;

    final res = await super.dio.post(
      '/api-key/create',
      data: body,
      options: await super.getOptions(isTokenRequired: true),
    );
    return ApiKey.fromJson(res.data);
  } catch (e) {
    final message = getErrorMessage(e);
    if (message == null) rethrow;
    throw message;
  }
}