Is it possible to generate API keys in bulk for users through a custom script in Frappe?

Hi everyone,

I wanted to confirm something regarding API key generation in Frappe/ERPNext.

I already checked with the Frappe support team, and they confirmed that there’s no built-in feature to generate API Keys and API Secrets in bulk for multiple users.

However, I wanted to know if it’s possible to achieve this through a custom server-side script — for example, by using generate_keys(user) in a loop for multiple users.

If yes, could anyone please share the recommended way or best practice to implement this safely?
(Mainly for internal use, not public API exposure.)

Thanks in advance,
Inshad

I think you are right.

But you can’t use import statement in server script.

So you have to implement by yourself.

@frappe.whitelist(methods=["POST"])
def generate_keys(user: str):
	"""
	generate api key and api secret

	:param user: str
	"""
	frappe.only_for("System Manager")
	user_details: User = frappe.get_doc("User", user)
	api_secret = frappe.generate_hash(length=15)
	# if api key is not set generate api key
	if not user_details.api_key:
		api_key = frappe.generate_hash(length=15)
		user_details.api_key = api_key
	user_details.api_secret = api_secret
	user_details.save()

	return {"api_key": user_details.api_key, "api_secret": api_secret}

you can make changes by accepting user list and using for loop generate at a same time and update response with key as user and value as secret and key.

and if you have custom app, it would be more better to add this to an App

When generating API keys in bulk, just keep it simple. Do it only for internal or trusted users, skip anyone who already has keys, and don’t print or expose the API secret anywhere. Run the script safely from the bench console or a server script, and keep track of who has access so you can revoke or update keys when needed.