| 1 | # Git LFS Batch API |
| 2 | |
| 3 | Added: v0.6 |
| 4 | |
| 5 | The Batch API is used to request the ability to transfer LFS objects with the |
| 6 | LFS server. The Batch URL is built by adding `/objects/batch` to the LFS server |
| 7 | URL. |
| 8 | |
| 9 | Git remote: https://git-server.com/foo/bar</br> |
| 10 | LFS server: https://git-server.com/foo/bar.git/info/lfs<br> |
| 11 | Batch API: https://git-server.com/foo/bar.git/info/lfs/objects/batch |
| 12 | |
| 13 | See the [Server Discovery doc](./server-discovery.md) for more info on how LFS |
| 14 | builds the LFS server URL. |
| 15 | |
| 16 | All Batch API requests use the POST verb, and require the following HTTP |
| 17 | headers. The request and response bodies are JSON. |
| 18 | |
| 19 | Accept: application/vnd.git-lfs+json |
| 20 | Content-Type: application/vnd.git-lfs+json |
| 21 | |
| 22 | The client may also include a `charset=utf-8` parameter in the |
| 23 | `Content-Type` header, which servers should be prepared to accept. |
| 24 | |
| 25 | See the [Authentication doc](./authentication.md) for more info on how LFS |
| 26 | gets authorizes Batch API requests. |
| 27 | |
| 28 | ## Requests |
| 29 | |
| 30 | The client sends the following information to the Batch endpoint to transfer |
| 31 | some objects: |
| 32 | |
| 33 | * `operation` - Should be `download` or `upload`. |
| 34 | * `transfers` - An optional Array of String identifiers for transfer adapters |
| 35 | that the client has configured. If omitted, the `basic` transfer adapter MUST |
| 36 | be assumed by the server. |
| 37 | * `ref` - Optional object describing the server ref that the objects belong to. Note: Added in v2.4. |
| 38 | * `name` - Fully-qualified server refspec. |
| 39 | * `objects` - An Array of objects to download. |
| 40 | * `oid` - String OID of the LFS object. |
| 41 | * `size` - Integer byte size of the LFS object. Must be at least zero. |
| 42 | * `hash_algo` - The hash algorithm used to name Git LFS objects. Optional; |
| 43 | defaults to `sha256` if not specified. |
| 44 | |
| 45 | Note: Git LFS currently only supports the `basic` transfer adapter. This |
| 46 | property was added for future compatibility with some experimental transfer |
| 47 | adapters. See the [API README](./README.md) for a list of the documented |
| 48 | transfer adapters. |
| 49 | |
| 50 | ```json5 |
| 51 | // POST https://lfs-server.com/objects/batch |
| 52 | // Accept: application/vnd.git-lfs+json |
| 53 | // Content-Type: application/vnd.git-lfs+json |
| 54 | // Authorization: Basic ... (if needed) |
| 55 | { |
| 56 | "operation": "download", |
| 57 | "transfers": [ "basic" ], |
| 58 | "ref": { "name": "refs/heads/main" }, |
| 59 | "objects": [ |
| 60 | { |
| 61 | "oid": "12345678", |
| 62 | "size": 123 |
| 63 | } |
| 64 | ], |
| 65 | "hash_algo": "sha256" |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | #### Ref Property |
| 70 | |
| 71 | The Batch API added the `ref` property in LFS v2.4 to support Git server authentication schemes that take the refspec into account. Since this is |
| 72 | a new addition to the API, servers should be able to operate with a missing or null `ref` property. |
| 73 | |
| 74 | Some examples will illustrate how the `ref` property can be used. |
| 75 | |
| 76 | * User `owner` has full access to the repository. |
| 77 | * User `contrib` has readonly access to the repository, and write access to `refs/heads/contrib`. |
| 78 | |
| 79 | ```json5 |
| 80 | { |
| 81 | "operation": "download", |
| 82 | "transfers": [ "basic" ], |
| 83 | "objects": [ |
| 84 | { |
| 85 | "oid": "12345678", |
| 86 | "size": 123 |
| 87 | } |
| 88 | ] |
| 89 | } |
| 90 | ``` |
| 91 | |
| 92 | With this payload, both `owner` and `contrib` can download the requested object, since they both have read access. |
| 93 | |
| 94 | ```json5 |
| 95 | { |
| 96 | "operation": "upload", |
| 97 | "transfers": [ "basic" ], |
| 98 | "objects": [ |
| 99 | { |
| 100 | "oid": "12345678", |
| 101 | "size": 123 |
| 102 | } |
| 103 | ] |
| 104 | } |
| 105 | ``` |
| 106 | |
| 107 | With this payload, only `owner` can upload the requested object. |
| 108 | |
| 109 | ```json5 |
| 110 | { |
| 111 | "operation": "upload", |
| 112 | "transfers": [ "basic" ], |
| 113 | "ref": { "name": "refs/heads/contrib" }, |
| 114 | "objects": [ |
| 115 | { |
| 116 | "oid": "12345678", |
| 117 | "size": 123 |
| 118 | } |
| 119 | ] |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | Both `owner` and `contrib` can upload the request object. |
| 124 | |
| 125 | ### Successful Responses |
| 126 | |
| 127 | The Batch API should always return with a 200 status, unless there are some |
| 128 | issues with the request (bad authorization, bad json, etc). See below for examples of response errors. Check out the documented transfer adapters in the |
| 129 | [API README](./README.md) to see how Git LFS handles successful Batch responses. |
| 130 | |
| 131 | Successful responses include the following properties: |
| 132 | |
| 133 | * `transfer` - String identifier of the transfer adapter that the server |
| 134 | prefers. This MUST be one of the given `transfer` identifiers from the request. |
| 135 | Servers can assume the `basic` transfer adapter if none were given. The Git LFS |
| 136 | client will use the `basic` transfer adapter if the `transfer` property is |
| 137 | omitted. |
| 138 | * `objects` - An Array of objects to download. |
| 139 | * `oid` - String OID of the LFS object. |
| 140 | * `size` - Integer byte size of the LFS object. Must be at least zero. |
| 141 | * `authenticated` - Optional boolean specifying whether the request for this |
| 142 | specific object is authenticated. If omitted or false, Git LFS will attempt |
| 143 | to [find credentials for this URL](./authentication.md). |
| 144 | * `actions` - Object containing the next actions for this object. Applicable |
| 145 | actions depend on which `operation` is specified in the request. How these |
| 146 | properties are interpreted depends on which transfer adapter the client will |
| 147 | be using. |
| 148 | * `href` - String URL to download the object. |
| 149 | * `header` - Optional hash of String HTTP header key/value pairs to apply |
| 150 | to the request. |
| 151 | * `expires_in` - Whole number of seconds after local client time when |
| 152 | transfer will expire. Preferred over `expires_at` if both are provided. |
| 153 | Maximum of 2147483647, minimum of -2147483647. |
| 154 | * `expires_at` - String uppercase RFC 3339-formatted timestamp with second |
| 155 | precision for when the given action expires (usually due to a temporary |
| 156 | token). |
| 157 | * `hash_algo` - The hash algorithm used to name Git LFS objects for this |
| 158 | repository. Optional; defaults to `sha256` if not specified. |
| 159 | |
| 160 | Download operations MUST specify a `download` action, or an object error if the |
| 161 | object cannot be downloaded for some reason. See "Response Errors" below. |
| 162 | |
| 163 | Upload operations can specify an `upload` and a `verify` action. The `upload` |
| 164 | action describes how to upload the object. If the object has a `verify` action, |
| 165 | the LFS client will hit this URL after a successful upload. Servers can use this |
| 166 | for extra verification, if needed. If a client requests to upload an object that |
| 167 | the server already has, the server should omit the `actions` property |
| 168 | completely. The client will then assume the server already has it. |
| 169 | |
| 170 | ```json5 |
| 171 | // HTTP/1.1 200 Ok |
| 172 | // Content-Type: application/vnd.git-lfs+json |
| 173 | { |
| 174 | "transfer": "basic", |
| 175 | "objects": [ |
| 176 | { |
| 177 | "oid": "1111111", |
| 178 | "size": 123, |
| 179 | "authenticated": true, |
| 180 | "actions": { |
| 181 | "download": { |
| 182 | "href": "https://some-download.com", |
| 183 | "header": { |
| 184 | "Key": "value" |
| 185 | }, |
| 186 | "expires_at": "2016-11-10T15:29:07Z" |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | ], |
| 191 | "hash_algo": "sha256" |
| 192 | } |
| 193 | ``` |
| 194 | |
| 195 | If there are problems accessing individual objects, servers should continue to |
| 196 | return a 200 status code, and provide per-object errors. Here is an example: |
| 197 | |
| 198 | ```json5 |
| 199 | // HTTP/1.1 200 Ok |
| 200 | // Content-Type: application/vnd.git-lfs+json |
| 201 | { |
| 202 | "transfer": "basic", |
| 203 | "objects": [ |
| 204 | { |
| 205 | "oid": "1111111", |
| 206 | "size": 123, |
| 207 | "error": { |
| 208 | "code": 404, |
| 209 | "message": "Object does not exist" |
| 210 | } |
| 211 | } |
| 212 | ], |
| 213 | "hash_algo": "sha256" |
| 214 | } |
| 215 | ``` |
| 216 | |
| 217 | LFS object error codes should match HTTP status codes where possible: |
| 218 | |
| 219 | * 404 - The object does not exist on the server. |
| 220 | * 409 - The specified hash algorithm disagrees with the server's acceptable options. |
| 221 | * 410 - The object was removed by the owner. |
| 222 | * 422 - Validation error. |
| 223 | |
| 224 | ### Response Errors |
| 225 | |
| 226 | LFS servers can respond with these other HTTP status codes: |
| 227 | |
| 228 | * 401 - The authentication credentials are needed, but were not sent. Git LFS |
| 229 | will attempt to [get the authentication](./authentication.md) for the request |
| 230 | and retry immediately. |
| 231 | * 403 - The user has **read**, but not **write** access. Only applicable when |
| 232 | the `operation` in the request is "upload." |
| 233 | * 404 - The Repository does not exist for the user. |
| 234 | * 422 - Validation error with one or more of the objects in the request. This |
| 235 | means that _none_ of the requested objects to upload are valid. |
| 236 | |
| 237 | Error responses will not have an `objects` property. They will only have: |
| 238 | |
| 239 | * `message` - String error message. |
| 240 | * `request_id` - Optional String unique identifier for the request. Useful for |
| 241 | debugging. |
| 242 | * `documentation_url` - Optional String to give the user a place to report |
| 243 | errors. |
| 244 | |
| 245 | ```json5 |
| 246 | // HTTP/1.1 404 Not Found |
| 247 | // Content-Type: application/vnd.git-lfs+json |
| 248 | |
| 249 | { |
| 250 | "message": "Not found", |
| 251 | "documentation_url": "https://lfs-server.com/docs/errors", |
| 252 | "request_id": "123" |
| 253 | } |
| 254 | ``` |
| 255 | |
| 256 | HTTP 401 responses should include an `LFS-Authenticate` header to tell the |
| 257 | client what form of authentication it requires. If omitted, Git LFS will assume |
| 258 | Basic Authentication. This mirrors the standard `WWW-Authenticate` header with |
| 259 | a custom header key so it does not trigger password prompts in browsers. |
| 260 | |
| 261 | ```json5 |
| 262 | // HTTP/1.1 401 Unauthorized |
| 263 | // Content-Type: application/vnd.git-lfs+json |
| 264 | // LFS-Authenticate: Basic realm="Git LFS" |
| 265 | |
| 266 | { |
| 267 | "message": "Credentials needed", |
| 268 | "documentation_url": "https://lfs-server.com/docs/errors", |
| 269 | "request_id": "123" |
| 270 | } |
| 271 | ``` |
| 272 | |
| 273 | The following status codes can optionally be returned from the API, depending on |
| 274 | the server implementation. |
| 275 | |
| 276 | * 406 - The Accept header needs to be `application/vnd.git-lfs+json`. |
| 277 | * 413 - The batch API request contained too many objects or the request was |
| 278 | otherwise too large. |
| 279 | * 429 - The user has hit a rate limit with the server. Though the API does not |
| 280 | specify any rate limits, implementors are encouraged to set some for |
| 281 | availability reasons. |
| 282 | * 501 - The server has not implemented the current method. Reserved for future |
| 283 | use. |
| 284 | * 507 - The server has insufficient storage capacity to complete the request. |
| 285 | * 509 - The bandwidth limit for the user or repository has been exceeded. The |
| 286 | API does not specify any bandwidth limit, but implementors may track usage. |
| 287 | |
| 288 | Some server errors may trigger the client to retry requests, such as 500, 502, |
| 289 | 503, and 504. |