ChatBotAPIKey Model

class smarter.apps.chatbot.models.ChatBotAPIKey(*args, **kwargs)[source]

Bases: TimestampedModel

Represents the mapping of API keys to ChatBot instances within the Smarter platform.

Important

If present, the ChatBot associated with this record will require Api Key authentication for all API requests. Otherwise, the ChatBot will allow anonymous unauthenticated access.

See smarter.lib.drf.token_authentication.SmarterTokenAuthentication .

This model establishes a relationship between a ChatBot and its associated API keys, enabling secure authentication and authorization for API access. Each entry in this model links a specific ChatBot to a unique API key, allowing fine-grained control over which keys can interact with which chatbot instances.

The ChatBotAPIKey model is essential for managing access to chatbot APIs, supporting use cases such as per-bot API key rotation, revocation, and auditing. By associating API keys with individual chatbots, the platform can enforce security policies and monitor usage at the chatbot level.

Typical usage involves creating a ChatBotAPIKey instance whenever a new API key is provisioned for a chatbot, and querying this model to validate incoming requests against active keys.

Model Relationships

  • Each ChatBotAPIKey is linked to one ChatBot instance.

  • Each ChatBotAPIKey references one SmarterAuthToken representing the API key.

Example

# Assign an API key to a chatbot
api_key = SmarterAuthToken.objects.create(...)
chatbot_api_key = ChatBotAPIKey.objects.create(chatbot=my_chatbot, api_key=api_key)

# Query API keys for a chatbot
keys = ChatBotAPIKey.objects.filter(chatbot=my_chatbot)

Notes

  • API key activation and deactivation are managed via the SmarterAuthToken model.

  • This model supports auditing and access control for chatbot API endpoints.

  • Intended for internal use within the Smarter platform to secure chatbot APIs.

Parameters:

Relationship fields:

Parameters:
  • chatbot (ForeignKey to ChatBot) –

    Chatbot (related name: chatbotapikey)

    The ChatBot instance associated with this function. Example: ChatBot(id=1, name=”my-chatbot”)

  • api_key (ForeignKey to SmarterAuthToken) –

    Api key (related name: chatbotapikey)

    The API key (SmarterAuthToken) associated with the ChatBot.

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

api_key

ForeignKey to SmarterAuthToken

Api key (related name: chatbotapikey)

The API key (SmarterAuthToken) associated with the ChatBot.

Type:

Type

api_key_id

Internal field, use api_key instead.

chatbot

ForeignKey to ChatBot

Chatbot (related name: chatbotapikey)

The ChatBot instance associated with this API key.

Type:

Type

chatbot_id

Internal field, use chatbot instead.

created_at

DateTimeField

Created at

Timestamp indicating when the model instance was created. This field is automatically set to the current date and time when the instance is first created. It is indexed in the database for efficient querying.

Type:

Type

classmethod get_cached_objects(invalidate=False, chatbot=None)[source]

Retrieve a list of ChatBotAPIKey instances associated with a ChatBot using caching.

Example usage:

# Retrieve API keys for a chatbot with caching
api_keys = ChatBotAPIKey.get_cached_objects(my_chatbot, invalidate=True)
Parameters:
  • invalidate (bool, optional) – Whether to invalidate the cache for this retrieval.

  • chatbot (ChatBot, optional) – The ChatBot instance for which to retrieve API keys.

Returns:

A queryset of ChatBotAPIKey instances associated with the ChatBot.

Return type:

models.QuerySet[“ChatBotAPIKey”]

classmethod has_active_api_key(chatbot, invalidate=False)[source]

Returns True if the chatbot has at least one active API key.

Return type:

bool

id

BigAutoField

Primary key: ID

Type:

Type

objects = <django.db.models.Manager object>
updated_at

DateTimeField

Updated at

Timestamp indicating when the model instance was last updated. This field is automatically updated to the current date and time whenever the instance is saved. It is indexed in the database for efficient querying.

Type:

Type