Create multiple trees via REST API issue

Gramps 5.2.2
Windows 11 docker with LetsEncrypt.

Site is up and running fine in Single Tree Mode. Running on a machine behind my modem with a Type A record pointing to it and port forwarding to the machine.

One admin account only, no users yet and no data imported.
currently logged in as admin.

Now, I’m trying to create a multi-tree setup as my Gramps Desktop have two trees in it I ultimately want to sync. I have a JSON file named ‘tree.json’ like so:

{
   "name":   "family_1"
}

and trying to create the first tree via the RESTful API like so:

curl -X POST "https://family-site.org/api/trees/" -H "accept: application/json" -H "Authorization: Bearer %GRAMPS_JWT%" -d @tree.json

and getting the following error messages:

{"error":{"code":422,"message":"json: name: Missing data for required field.","messages":{"json":{"name":["Missing data for required field."]}}}}

and I can’t format the json in any config that works. I’ve tried the json structure on the command line before trying this external file method. I’ve also tried:

{
  "json": {"name": ["family_1"]}
}
{
  "name":  ["family_1"]
}

I will keep experimenting, I’ve been to the REST API reference site, as has chatGPT and I just can’t seem to make any progress and would appreciate any insight that someone may have.

On a side note, I first tried to set TREE: * in the yaml file and when I did, I could not sign into the instance any longer with my admin credentials. So it is my assumption that I need to sign in as an admin (site up), then create the multiple trees via REST, and then create owners for those tree, then set the TREE “*” config option and THEN I can sign in somehow with the owner credentials? and in that case, what/how are the admin credentials used for?

I’m SO close to getting this up and running and my family is all waiting to submit stuff, quite exciting. Thx in advance for any insights / help.

Yep, you’re close.

I personally hate curl. It would be easier with Python requests.

But you can try this

curl -X POST "https://family-site.org/api/trees/" \
-H "Authorization: Bearer $GRAMPS_JWT" \
-H "Content-Type: application/json" \
-d '{"name": "family_1"}'

Thx David, I’ll try that. I’m right there with you with curl; however, getting the gramps_webapi going on Windows 11 had its own headaches at the time, so I opted for the quick out with curl in the interest of impatience. I will probably revisit getting gramps_webapi going before this is all over. Thx again.

@DavidMStraub

Thx David, I did get curl going with a -g arg, but need a little guidance on moving forward.

I began my install with a single-tree setup and created an admin account. Now, I want to setup a multi-tree config. When I set TREE: “*” in the yaml file however, then my admin credentials no longer work, they are “forbidden” when I try and obtain an authorization token to move forward with new tree creation…, so I cannot create new trees via the webapi.

I am unsure how to back out of this situation. Attempts to create a new admin while in multi-tree mode result in “code 405 Users already exist”. Deleting the admin in single-user mode and performing a new “first-run” in multi-tree mode still won’t take the admin credentials created there.

With the credentials not being accepted for token generation in multi mode, I am at a loss for direction on how to proceed. I welcome any strategic recommendations you may have on a sequence of actions.

When doing the first run in multi-tree mode and creating the admin, I continually get this. Not sure how I can have a wrong user name or password at the time I’m actually creating it…and to make things more fun, as soon as I do create it, I can’t log in with it. *sigh! This only happens in multi-tree mode.

Another point of confusion. Being a Fresh Install, and not importing any trees yet…when, (through the webAPI) I list the trees, I get a “Gramps Web” result. Ultimately I’m wanting to create two new trees for: “family_1” and “family_2”, not 'Gramps Web", so I have no idea where that comes from or what to do with it".

Please share the logs.

Btw you can create users also via command line, see User system - Gramps Web

I’ll try and find them.

In the meantime, are the following statements correct assumptions?

. The admin credentials are supposed to work for obtaining tokens via webAPI, no matter if you created the admin credentials in single-tree or multi-tree mode.

. To create a multi-tree, I should first create an admin account, in single-tree or multi-tree it doesn’t matter. Once done, I can (or should) be able to use the webAPI to get a new token against those admin credentials and creatre new trees and owners for those trees.

No - all user accounts are tied to a tree. So you can’t create a user that can access more than one tree. The admin can create new users for any tree though.

And, another gotcha: the onboarding dialog in the single tree setup creates a user with owner permissions, not with site administrator permissions. So if you switch to multi-tree afterwards, that user cannot administrate other trees. You need to create a new user on the command line in that case.

I’ll try creating a new user with Role ID : 5 I assume that’s the way to go about it?

@DavidMStraub

I revisited the webapi docs and gained a bit more clarity, but have some additional questionsl Here’s the steps I’ve performed thus far in trying to create a site-admin, all using the restAPI

  1. deleted all users to start fresh. Confirmed by both getting an empty list of users on the command line as well as seeing the “create admin screen”.
  2. Configured the TREE: “*” in the YAML file.
  3. Got an admin token by POST to /token/create_owner/ and passing NO tree parameter.
  4. Create an admin user by POST to /users/{user_name}/create_owner/. Passed the JSON payload with NO tree parameter…because as I understand it this would create an OWNER and not a site admin. This seemed to work, I got no error messages on the command line.

I then confirmed the creating of “a user” by refreshing the site and no longer do I see the “create admin” screen, but now a simple login screen. This tells me there is a user in the database, presumably a site-admin

  1. Enter the newly created ‘admin’ username / password

I get a WRONG USERNAME / PASSWORD message.

What steps above to create a site-administrator are wrong? Did I do the right steps to create a site-admin? what did I miss if not. It seems this user is an OWNER, though the users “tree” parametr is “null”.

The user is role: 5

Your API docs consistently say “owner or admin”, but never distinguish what causes one or the other to be created and as we’re seeing here, the difference between the two are pretty significant.

I am using your Swagger UI tool to create the rest command, but still cannot successfully create a site-administor using the /create_owner/ endpoints.

The multi-tree setup is currently not currently not very well documented and it’s not possible to manage from the UI. There are open bug & feature reports, e.g.

The reason you can’t log in is because the site admin user has an empty tree ID by default. To understand why that’s the case, you have to know that the multi-tree feature was not originally developed to allow a single user to host multiple trees. It was developed to allow a single Gramps Web installation to serve multiple tenants, in a way that is as isolated as possible to preserve individual tree’s privacy. That’s also why it’s currently not possible to share data/users between trees. That’s something we might want to add in the future, but it’s not trivial because we want to keep the isolation the default.

So, when you create and admin for a multi-tenant installation, you can manage trees, but you don’t have access to a tree by default. For illustration: Grampshub has an admin account that can delete & create trees but cannot access Grampshub user’s data!

What you should do as next step, then, is to create a tree owner for a specific tree. Then you can log in.

1 Like

Alternatively, you can PUT your admin user and add a tree - then the admin will be able to login to this tree (but no other).

1 Like

Thank you @DavidMStraub , that was a great explanation!

I will give that a try.