Documentation about master key

On the documentation section Initialize Moralis it says

“If the master key needs to be provided, use the following”

Maybe you could elaborate on this? In what case would the master key be provided?

2 Likes

Which part of the documentation are you referring to? could you provide us with the link?

You will not want to provide the master key in client side javascript. With the master key you can do any updates in the database and any selects. You can use the master key in a cloud function, that is where you’ll use it most of the time, and there you use it with useMasterKey parameter for a query.

1 Like

I also think that the documentation for master key is a bit confusing.
From my understanding master key is used if you have some other backend from where you can safely make the calls. I also assume that master key is available by default in the cloud functions, however, this is not written anywhere.

Moreover, documentation about master key https://docs.moralis.io/moralis-server/cloud-code/cloud-functions#calling-cloud-functions

query.find({useMasterKey:true});
object.save(null,{useMasterKey:true});
Moralis.Object.saveAll(objects,{useMasterKey:true});

doesn’t give any info on how to use the master key from a custom backend server that we are controlling and running.
Additionally, it’s not also clear how to pass master key if we are calling the cloud function.

1 Like

We can add an example in documentation on how to use it from backend, for example in this tutorial code you can find an example on how to use it: https://github.com/ashbeech/moralis-mutants-nft-engine/blob/fef49e26c4617e93957456ee19c58cf645687589/index.js#L26

Also, now anyone can contribute to Moralis documentation here: https://github.com/MoralisWeb3/MoralisDocumentation

1 Like

Thx, that is quite helpful. I can submit a pr for the documentation. However, can you explain it to me if is master user accessible by default in cloud functions when you use useMasterKey:true or it needs to be set somewhere?

It is accessible by default in a cloud function when you use useMasterKey:true

Hello @cryptokid, I tried using the code from the github link that you sent. However, it doesn’t work!

In the code itself it doesnt seem that start function accepts masterKey as argument ( https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/a495cc9c2f34eaefbc7270b9c47bd00e449daddd/src/Parse.js#L34)

The error i get is: ParseError: Cannot access 'query' before initialization

1 Like

it looks like I see something different here: https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/d02580ffaa6706182af0d4489f6e8faf9ede9b08/src/Parse.js#L35

Thx, I fixed it. It seems I was using the query before declaring it.