Users.labels: create

Requires authorization

Creates a new label. Try it now or see an example.

Request

HTTP request

POST https://www.googleapis.com/gmail/v1/users/userId/labels

Parameters

Parameter name Value Description
Path parameters
userId string The user's email address. The special value me can be used to indicate the authenticated user.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.labels

Request body

In the request body, supply a Users.labels resource with the following properties:

Property name Value Description Notes
Required Properties
labelListVisibility string The visibility of the label in the label list in the Gmail web interface.

Acceptable values are:
  • "labelHide": Do not show the label in the label list.
  • "labelShow": Show the label in the label list. (Default)
  • "labelShowIfUnread": Show the label if there are any unread messages with that label.
writable
messageListVisibility string The visibility of messages with this label in the message list in the Gmail web interface.

Acceptable values are:
  • "hide": Do not show the label in the message list.
  • "show": Show the label in the message list. (Default)
writable
name string The display name of the label. writable
Optional Properties
color nested object The color to assign to the label. Color is only available for labels that have their type set to user. writable
color.backgroundColor string The background color represented as hex string #RRGGBB (ex #000000). This field is required in order to set the color of a label. Only the following predefined set of color values are allowed:
#000000, #434343, #666666, #999999, #cccccc, #efefef, #f3f3f3, #ffffff, #fb4c2f, #ffad47, #fad165, #16a766, #43d692, #4a86e8, #a479e2, #f691b3, #f6c5be, #ffe6c7, #fef1d1, #b9e4d0, #c6f3de, #c9daf8, #e4d7f5, #fcdee8, #efa093, #ffd6a2, #fce8b3, #89d3b2, #a0eac9, #a4c2f4, #d0bcf1, #fbc8d9, #e66550, #ffbc6b, #fcda83, #44b984, #68dfa9, #6d9eeb, #b694e8, #f7a7c0, #cc3a21, #eaa041, #f2c960, #149e60, #3dc789, #3c78d8, #8e63ce, #e07798, #ac2b16, #cf8933, #d5ae49, #0b804b, #2a9c68, #285bac, #653e9b, #b65775, #822111, #a46a21, #aa8831, #076239, #1a764d, #1c4587, #41236d, #83334c
writable
color.textColor string The text color of the label, represented as hex string. This field is required in order to set the color of a label. Only the following predefined set of color values are allowed:
#000000, #434343, #666666, #999999, #cccccc, #efefef, #f3f3f3, #ffffff, #fb4c2f, #ffad47, #fad165, #16a766, #43d692, #4a86e8, #a479e2, #f691b3, #f6c5be, #ffe6c7, #fef1d1, #b9e4d0, #c6f3de, #c9daf8, #e4d7f5, #fcdee8, #efa093, #ffd6a2, #fce8b3, #89d3b2, #a0eac9, #a4c2f4, #d0bcf1, #fbc8d9, #e66550, #ffbc6b, #fcda83, #44b984, #68dfa9, #6d9eeb, #b694e8, #f7a7c0, #cc3a21, #eaa041, #f2c960, #149e60, #3dc789, #3c78d8, #8e63ce, #e07798, #ac2b16, #cf8933, #d5ae49, #0b804b, #2a9c68, #285bac, #653e9b, #b65775, #822111, #a46a21, #aa8831, #076239, #1a764d, #1c4587, #41236d, #83334c
writable

Response

If successful, this method returns a Users.labels resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Label;

import java.io.IOException;

// ...

public class MyClass {

  // ...


  /**
   * Add a new Label to user's inbox.
   *
   * @param service Authorized Gmail API instance.
   * @param userId User's email address. The special value "me"
   * can be used to indicate the authenticated user.
   * @param newLabelName Name of the new label.
   * @throws IOException
   */
  public static Label createLabel(Gmail service, String userId, String newLabelName)
      throws IOException {
    Label label = new Label().setName(newLabelName);
    label = service.users().labels().create(userId, label).execute();

    System.out.println("Label id: " + label.getId());
    System.out.println(label.toPrettyString());

    return label;
  }

  // ...

}

.NET

Uses the .NET client library.

using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// Add a new Label to user's mailbox.
  /// </summary>
  /// <param name="service">Gmail API service instance.</param>
  /// <param name="userId">User's email address. The special value "me"
  /// can be used to indicate the authenticated user.</param>
  /// <param name="newLabelName">Name of the new Label.</param>
  public static Label CreateLabel(GmailService service, String userId, String newLabelName)
  {
      Label label = new Label();
      label.Name = newLabelName;
      try
      {
          return service.Users.Labels.Create(label, userId).Execute();
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }
      return null;
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Add a new Label to user's mailbox.
 *
 * @param  Google_Service_Gmail $service Authorized Gmail API instance.
 * @param  string $userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  string $new_label_name Name of the new Label.
 * @return Google_Service_Gmail_Label Created Label.
 */
function createLabel($service, $user, $new_label_name) {
  $label = new Google_Service_Gmail_Label();
  $label->setName($new_label_name);
  try {
    $label = $service->users_labels->create($user, $label);
    print 'Label with ID: ' . $label->getId() . ' created.';
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }
  return $label;
}

Python

Uses the Python client library.

"""Create and add label to user's account.
"""

from apiclient import errors


def CreateLabel(service, user_id, label_object):
  """Creates a new label within user's mailbox, also prints Label ID.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    label_object: label to be added.

  Returns:
    Created Label.
  """
  try:
    label = service.users().labels().create(userId=user_id,
                                            body=label_object).execute()
    print label['id']
    return label
  except errors.HttpError, error:
    print 'An error occurred: %s' % error


def MakeLabel(label_name, mlv='show', llv='labelShow'):
  """Create Label object.

  Args:
    label_name: The name of the Label.
    mlv: Message list visibility, show/hide.
    llv: Label list visibility, labelShow/labelHide.

  Returns:
    Created Label.
  """
  label = {'messageListVisibility': mlv,
           'name': label_name,
           'labelListVisibility': llv}
  return label

JavaScript

Does not use a client library.

/**
 * Add a new Label to user's mailbox.
 *
 * @param  {String} userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  {String} newLabelName Name of the new Label.
 * @param  {Function} callback Function to call when the request is complete.
 */
function createLabel(userId, newLabelName, callback) {
  var request = gapi.client.gmail.users.labels.create({
    'userId': userId,
    'label': {
      'name': newLabelName
    }
  });
  request.execute(callback);
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response.

フィードバックを送信...

ご不明な点がありましたら、Google のサポートページをご覧ください。