attachment: createUploadSession

Important

APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported.

Create an upload session that allows an app to iteratively upload ranges of a file, so as to attach the file to the specified message.

Use this approach to attach files of sizes between 3 MB and 150 MB to a message. To attach files of sizes under 4 MB, simply POST on the attachments navigation property.

As part of the response, this action returns an upload URL that you can use in subsequent sequential PUT queries. Request headers for each PUT operation let you specify the exact range of bytes to be uploaded. This allows transfer to be resumed, in case the network connection is dropped during upload.

The following are the steps to attach a file using an upload session:

  1. Create an upload session
  2. Within that upload session, iteratively upload ranges of bytes (up to 4 MB each time) until all the bytes of the file have been uploaded, and the file is attached to the specified message
  3. Save the ID for the attachment for future access
  4. Optional: Delete the upload session

See attach large files to Outlook messages for an example.

Tip

Exchange Online lets administrators customize the message size limit for Office 365 mailboxes, including any message attachments. By default, this message size limit is 35 MB. Find out how to customize the maximum message size to support attachments larger than the default limit for your tenant.

Permissions

One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions.

Permission type Permissions (from least to most privileged)
Delegated (work or school account) Mail.ReadWrite
Delegated (personal Microsoft account) Mail.ReadWrite
Application Mail.ReadWrite

HTTP request

POST /me/messages/{id}/attachments/createUploadSession

Request headers

Name Description
Authorization Bearer {token}

Request body

In the request body, provide a JSON object with the following parameters.

Parameter Type Description
AttachmentItem attachmentItem Represents attributes of the item to be uploaded and attached. At minimum, specify the attachment type (file), a name, and the size of the file.

Response

If successful, this method returns 201, Created response code and a new uploadSession object in the response body.

Note:

The uploadUrl property returned as part of the uploadSession response object is an opaque URL for subsequent PUT queries to upload byte ranges of the file. It contains the appropriate auth token for subsequent PUT queries that expire by expirationDateTime. Do not customize this URL.

The nextExpectedRanges property specifies the next file byte location to upload from, for example, "NextExpectedRanges":["2097152"]. You must upload bytes in a file in order.

Examples

The following is an example of how to call this API.

Request

The following is an example of the request.

POST https://graph.microsoft.com/beta/me/messages/AAMkADI5MAAIT3drCAAA=/attachments/createUploadSession
Content-type: application/json

{
  "AttachmentItem": {
    "attachmentType": "file",
    "name": "flower", 
    "size": 3483322
  }
}

Response

The following is an example of the response.

Note: The response object shown here might be shortened for readability. All the properties will be returned from an actual call.

HTTP/1.1 201 Created
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#microsoft.graph.uploadSession",
    "uploadUrl": "https://outlook.office.com/api/beta/Users('a8e8e219-4931-95c1-b73d-62626fd79c32@72aa88bf-76f0-494f-91ab-2d7cd730db47')/Messages('AAMkADI5MAAIT3drCAAA=')/AttachmentSessions('AAMkADI5MAAIT3k0uAAA=')?authtoken=eyJhbGciOiJSUzI1NiIsImtpZCI6IktmYUNIUlN6bllHMmNI",
    "expirationDateTime": "2019-09-25T01:09:30.7671707Z",
    "nextExpectedRanges": [
        "0-"
    ]
}