Creating a cloud function that returns a the API Key

Hi Moralis community!

I want to be able to shield an API key, where only users can access a specific API Key to run something.

Basically: inputAPIKey() in the front-end would run a cloud function to then return in an input field the value of that API key.

What is the best way to create a simple Cloud Code function that I could use to return a value I define in my Cloud Code?


What did you try by now?

You can return something in an cloud function, you can also check the request where you can validate the user bases on the session key used

Hey! So I’ve tried to do a couple things: (1.) to get the total users in the db, and the (2.) to try to return a API Key (that would only show client side if {user === true}

Cloud Code

// Get total users
Moralis.Cloud.define("get_nr_users", async (request) => {
  query = new Moralis.Query("User");
  x = await query.count({useMasterKey : true});
  return x;
// Get Company API Key
Moralis.Cloud.define("get_company_api_key", async (request) => {
  return x;


In the front-end, I called the # of users function this way:

// state variable
const [numOfUsers, setNumOfUsers] = useState('');
// function'get_nr_users').then(res=> {
    // JSON.stringify turns response into string
    const users = JSON.stringify(res)
  }).catch(err=> console.log(err))

return (

However, I get this error:
Error: You need to call Moralis.start with an applicationId before using Moralis.

Do you know what I might be doing wrong to call the cloud function? The cloud-functions don’t return any errors in the Moralis server.

It looks like the react application was not properly initialized with server url and app id.

Yea it’s all set in the _app.tsx file via env. variables. I’m just wondering why I can’t request the value of the cloud function. Do you have a sense on what the right way to return the value of the cloud function is? As soon as I remove the cloud function get function, then the app works :sweat_smile:

It doesn’t look like a problem with that syntax. You could also try to use a hook specific to call cloud functions. You may need to initialize Moralis depending on how you imported it in that application.