Jeff McKay
Jeff McKay

Reputation: 295

How to create a sub label with Gmail REST API

I am using the Gmail REST API to create a label (using my own code, not a library). I have it working fine for a top level label, but now I want to create a sub label to the top label. I noticed that when I create a sub label using Gmail, then look at the list I receive via the REST API, sub labels are shown using the '/' character as a delimiter, e.g. "Top Label/Sub Label". However when I try to create a label like this, I just get a label that has the slash in the name, not an actual sub label. How can I do this?

Upvotes: 3

Views: 2320

Answers (2)

Brian Sanchez
Brian Sanchez

Reputation: 918

this is using python, and using MakeLabel and CreateLabel from here https://developers.google.com/gmail/api/v1/reference/users/labels/create

this is easy!..

if papa_label_dont_exists:
    gmail_label_obj  = MakeLabel('Father')
    label            = CreateLabel(service, "me", gmail_label_obj)

gmail_label_obj  = MakeLabel('Father/Son')
label            = CreateLabel(service, "me", gmail_label_obj)

Upvotes: 2

Jay Lee
Jay Lee

Reputation: 13528

You need to make two API calls. The first to create the top level label, the 2nd to create the child label. Once both labels exist, Gmail UI will properly collapse the label hierarchy.

Upvotes: 6

Related Questions