TrueNAS SCALETrueNAS SCALE Version Documentation
This content follows the TrueNAS SCALE 23.10 (Cobia) releases. Use the Product and Version selectors above to view content specific to different TrueNAS software or major version.

API_Key

  6 minute read.

Last Modified 2024-03-19 08:38 EDT

The SCALE CLI guide is a work in progress! New namespace and command documentation is continually added and maintained, so check back here often to see what is new!

API_Key Namespace

The api_key namespace has five commands and is based on API key management functions found in the SCALE API and web UI. It provides access to API key creation and management methods through the api_key commands.

API_Key Commands

The following api_key commands allow you to create, view, and delete API keys.

You can enter commands from the main CLI prompt or from the auth namespace prompt.

The SCALE CLI guide is a work in progress! New namespace and command documentation is continually added and maintained, so check back here often to see what is new!

Interactive Argument Editor (TUI)

Enter the -- flag following any CLI command to open the interactive arguments editor text-based user interface (TUI).

Create Command

The create command lets you create simple or complex API keys.

Description

The create command has one required property, name, and one optional property allowlist. See Create Command Properties below for more information on these properties. Enter the command string then press Enter. The command returns the API key when successful.

Always back up and secure keys. The key string displays only one time, at creation!
PropertyRequiredDescriptionSyntax Example
nameYesEnter a user-readable name for the API key using alphanumeric characters with or without the underscore _. Enter the property argument using the = to separate property and value.name=mykey
allowlistNoUse to enter the HTTP method and WebSocket API authorized to use the API key.
Enter the required resource permitted to use this key. Append /api/docs/ to the end of your TrueNAS web UI address to see our full list of WebSocket API resources.
Enter the HTTP method as a string using any of these values:
  • GETto retrieve information about the API resource.
  • POST to create an API resource.
  • PUT to update an API resource.
  • DELETE to delete the API resource.
  • CALL,or
  • SUBSCRIBE

  • Enclosed property arguments within curly brackets {} inside square brackets []. Enter property arguments using the = to separate double-quoted property and values. Separate each propery argument with a comma and space.
    allowlist=[{“method”="SUBSCRIBE", [“resource”="certificate.query"}]

    Usage

    From the CLI prompt, enter:

    auth api_key create name=name

    Where name is the name you want to assign to the key.

    auth api_key create name=apikey3
    API Key: 3-xTqwhyf3SrUgUlotMQEEGuUr6oRvqg89SBDfXob6xtWSgLbRiDBr6SVRWxswSXx3
    

    Description

    Enter the create command name property argument followed by the allowlist property argument. You can also specify an HTTP method and a WebSocket API method. Enter the command string then press Enter. The command returns the API key when successful.

    Always back up and secure keys. The key string displays only one time, at creation!

    Usage

    From the CLI prompt, enter:

    auth api_key create name=name allowlist=[{“method”:"METHOD",“resource”:"api.resource"}]

    Where:

    • name is the name you want to assign to the key.
    • METHOD is the HTTP method you want the key to use. Options are GET, POST, PUT, DELETE, CALL, and SUBSCRIBE.
    • api.resource is the WebSocket API resource you want to use. Append “/api/docs/” to the end of your TrueNAS web UI address to see our full list of WebSocket API resources.
    auth api_key create name=apikey3 allowlist=[{"method":"SUBSCRIBE","resource":"certificate.query"}]
    API Key: 3-xTqwhyf3SrUgUlotMQEEGuUr6oRvqg89SBDfXob6xtWSgLbRiDBr6SVRWxswSXx3
    

    Delete Command

    The delete command deletes an API key.

    Description

    The delete command has one required property, id. Enter the property argument using the = delimiter to separate the property and value. Enter the command then press Enter. The command returns nothing when successful.

    Usage

    From the CLI prompt, enter:

    auth api_key delete id=number

    Where number is the list number of the API key you want to delete. Use the query command to retrieve id numbers for all API keys on the system.

    auth api_key delete id=1
    

    Get_Instance Command

    The get_instance command returns a table of properties for the specified API key.

    Description

    The get_instance command has one required property, id. Enter the property argument using the = delimiter to separate the property and value. Enter the command then press Enter. The command returns a table of properties for the specified API key when successful.

    Usage

    From the CLI prompt, enter:

    auth api_key get_instance id=number

    Where number is the list number of the API key you want to delete. Use the query command to retrieve id numbers for all API keys on the system.

    auth api_key get_instance id=2
    +------------+---------------------------+
    |         id | 2                         |
    |       name | apikey2                   |
    | created_at | 2023-08-22T19:58:59+00:00 |
    |  allowlist | <list>                    |
    +------------+---------------------------+
    

    Query Command

    The query command returns a table of properties for all API keys.

    Description

    The query command does not require entering property arguments. Enter the command then press Enter. The command returns a table of properties for all API keys when successful.

    Usage

    From the CLI prompt, enter:

    auth api_key query

    auth api_key query
    +----+---------+---------------------------+-----------+
    | id | name    | created_at                | allowlist |
    +----+---------+---------------------------+-----------+
    | 2  | apikey2 | 2023-08-22T19:58:59+00:00 | <list>    |
    | 3  | apikey3 | 2023-08-22T20:14:59+00:00 | <list>    |
    | 4  | apikey4 | 2023-08-22T20:17:44+00:00 | <list>    |
    +----+---------+---------------------------+-----------+
    

    Update Command

    The update command allows you to update existing API keys.

    Description

    The update command has one required property, id, and three configurable properties. See Update Command Properties below for details. After specifying the id of the API key you want to update, you must include at least one of the properties. Enter -- after entering the id property argument to open the interactive argument editor. Enter the command string then press Enter. The command returns a table of properties for all API keys when successful.

    Usage

    From the CLI prompt, enter:

    auth api_key update id=number name=name allowlist=[{“method”:"METHOD",“resource”:"api.resource"}] reset=true/false

    Where

    • number is the list number of the API key you want to update. For example, the first API key created on the system would be 1.
    • name is the new name you want to assign to the key.
    • METHOD is the HTTP method you want the key to use. Options are GET, POST, PUT, DELETE, CALL, and SUBSCRIBE.
    • api.resource is the WebSocket API resource you want to use. Append “/api/docs/” to the end of your TrueNAS web UI address to see our full list of WebSocket API resources.
    • true/false determines if you want to remove the existing API key and generate a new random key.
    auth api_key update id=2 name=apikey3 allowlist=[{"method":"SUBSCRIBE","resource":"certificate.query"}] reset=true
    

    Related CLI Auth Articles

    Related API Key Articles