API Keys

Managing Machine Users and API Keys#

Machine users are service accounts that allow applications or services to interact with the API on behalf of an organization. This documentation outlines how to create, manage, and generate API keys for machine users.

Creating a Machine User#

To create a new machine user, use the hf users create-machine command followed by the name of your desired machine user identifier.

❯ hf users create-machine myproject
Created: machine-user-CMKQE5ZFLNDFFAV6Y4EDLV4H

This command will generate a machine user ID. Newly created machine users are initially disabled by default and must be granted permissions before they can interact with the API.

Listing Users#

To view all users, including machine users, use the hf users list command:

❯ hf users list
id org email enabled level
h8yB9f2rRng3o92Hva1L9MS88d32 math-test [email protected] true LEVEL_ADMIN
machine-user-CMKQE5ZFLNDFFAV6Y4EDLV4H math-test [email protected] false LEVEL_DENY

The output shows both human and machine users, along with their status and permission levels.

Enabling a Machine User#

Once created, you will need to enable the machine user to allow it to interact with the system. Use the appropriate command to update the machine user's status from LEVEL_DENY to an appropriate permission level. You may also edit the user's permission levels in the product's user interface.

Creating an API Key for a Machine User#

To generate an API key for a machine user, use the hf users create-key command, specifying the machine user ID and a name for the key.

❯ hf users --id machine-user-CMKQE5ZFLNDFFAV6Y4EDLV4H create-key mykey
eyJhbGciOiJFUzI1NiIsImtpZCI6ImFwaS1zdGFnaW5nLTEiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOlsiaHR0cHM6Ly9hcGktc3RhZ2luZy5odW1hbmZpcnN0LmFpIl0sImVtYWlsIjoibXlwcm9qZWN0LWNta3FlNSswZmUwM2E0OC02ZmYyLTQ3MDQtODhhMC03NDRlYjkzY2YzMzJAbWF0aC10ZXN0LnNlcnZpY2VhY2NvdW50Lmh1bWFuZmlyc3QuYWkiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiZXhwIjoyMDQ1NjY1OTU5LCJpc3MiOiJodHRwczovL2FwaS1zdGFnaW5nLmh1bWFuZmlyc3QuYWkiLCJqdGkiOiIwZmUwM2E0OC02ZmYyLTQ3MDQtODhhMC03NDRlYjkzY2YzMzIiLCJuYW1lIjoibXlrZXkiLCJvcmdhbml6YXRpb24iOiJtYXRoLXRlc3QiLCJzdWIiOiJtYWNoaW5lLXVzZXItQ01LUUU1WkZMTkRGRkFWNlk0RURMVjRILzBmZTAzYTQ4LTZmZjItNDcwNC04OGEwLTc0NGViOTNjZjMzMiJ9.iOfEtCy9yGbT2denqAvnF8fphcaGydRCUPEIAbyx3VUqF6GGVYT5isrMlnNHbD6Jh5JjoWddvYTqf9cJKYmA_Q

The command will output a newly generated API key. Make sure to store this key securely, as it is used to authenticate API requests.

You can also specify an expiration duration for the key using the --duration option (e.g., 24h, 7d, 1y). By default, the key does not expire.

Listing API Keys#

To list all API keys associated with a machine user, use the list-keys command:

❯ hf users --id machine-user-CMKQE5ZFLNDFFAV6Y4EDLV4H list-keys
Id Name Token Expires Last Used At
2 mykey ***f9cJKYmA_Q Never Never

This command displays the keys, including their names, expiration status, and last usage information.

Disabling or Deleting a Machine User or API Key#

If a machine user or its associated API key is no longer needed, ensure it is disabled or deleted to prevent unauthorized access.

❯ hf users --id machine-user-CMKQE5ZFLNDFFAV6Y4EDLV4H delete-key 2