Skip to content

UploadFile#

Test Postman

The method is intended for uploading a file to cloud storage, which can then be sent using the sendFileByUrl method. It also allows you to avoid errors when getting files from external storage and to speed up message sending, for example, in a bulk sending scenario.

An alternative way to send a file by uploading it from disk: sendFileByUpload.

It is recommended to use the method for bulk sending of similar messages:

  1. Use the UploadFile method to upload the required file to the Green-API storage, and get a link to the file in the response.
  2. Use the SendFileByUrl method to send it, using the received link.

The file storage period is 24 hours.

Request#

To upload a file, you need to execute a request at:

POST
{{mediaUrl}}/waInstance{{idInstance}}/uploadFile/{{apiTokenInstance}}

To get the mediaUrl, idInstance and apiTokenInstance request parameters, refer to the Before you start section.

Header parameters#

Parameter Required Description
Content-Type Yes File type according to the list of MIME types
If the file type is not in the list, then GA-Filename is passed instead of this line
GA-Filename No File name with the extension, if the file is not in the list of MIME types. For example, .go or .py files
If the file type matches the list, the line is not passed, only Content-Type is used

If the system cannot determine the file type, the file will be sent with the default type (a binary file): .bin

Request parameters#

Parameter Type Required Description
file binary Yes File being uploaded

Request body example#

The file itself must be specified as the request body

Response#

Response fields#

Field Type Description
urlFile string Link to the uploaded file (the link is valid for 24 hours)

Response body example#

{
    "urlFile": "https://sw-media-out.storage.yandexcloud.net/1101123456/c1aabd48-c1c2-49b1-8f2d-f575a41777be.jpg"
}

uploadFile errors#

For the list of errors common to all methods, see the Common errors section

HTTP code Error identifier Description
400 file should not be empty The user sent an empty file. The file being uploaded must not be empty.
413 request entity too large The permitted file size has been exceeded (~99.9mb)
Occurs when sending files in a 1C data processor (version 8.3.22.1923). Possible solution: change the 1C version.
500 Internal Server Error The request method is specified incorrectly. Change the method to POST and repeat the request

Code examples#

uploadFile code examples#

import requests
url = "{{mediaUrl}}/waInstance{{idInstance}}/uploadFile/{{apiTokenInstance}}"

headers = {
'Content-Type': 'image/jpeg'
}

with open('C:\\window.jpg', mode='rb') as file:
    payload = file.read()

response = requests.post(url, headers=headers, data=payload)

print(response.text.encode('utf8'))
curl --location '{{mediaUrl}}/{{idInstance}}/uploadFile/{{apiTokenInstance}}' \
--header 'Content-Type: image/jpeg' \
--data '@test.jpg'
var file = new File("/Users/user/Desktop/fileExample.jpeg");
var restTemplate = new RestTemplate();
var requestUrl = new StringBuilder();
requestUrl
    .append({{mediaUrl}})
    .append("/waInstance").append({{idInstance}})
    .append("/uploadFile/")
    .append({{apiTokenInstance}});

var byteArrayResource = new ByteArrayResource(Files.readAllBytes(file.toPath()));

var headers = new HttpHeaders();
headers.setContentType(MediaTypeFactory.getMediaType(file.getName())
    .orElse(MediaType.APPLICATION_OCTET_STREAM));

var requestEntity = new HttpEntity<>(byteArrayResource, headers);

var response = restTemplate.exchange(requestUrl.toString(), HttpMethod.POST, requestEntity, String.class);
System.out.println(response);
var file = new File("/Users/user/Desktop/fileExample.jpeg");
var requestUrl = new StringBuilder();
requestUrl
    .append({{mediaUrl}})
    .append("/waInstance").append({{idInstance}})
    .append("/uploadFile/")
    .append({{apiTokenInstance}});

var response = Unirest.post(requestUrl.toString())
    .header("Content-Type", Files.probeContentType(file.toPath()))
    .body(Files.readAllBytes(file.toPath()))
    .asString();

System.out.println(response);
<?php
require './vendor/autoload.php';

use GreenApi\RestApi\GreenApiClient;

define( "ID_INSTANCE", "1101712345" );
define( "API_TOKEN_INSTANCE", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" );

$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );

$result = $greenApi->sending->uploadFile(
    'C:\Games\PicFromDisk.png'
);

print_r(  $result->data );

Code example using the uploadFile + sendFileByUrl methods#

<?php
require './vendor/autoload.php';

use GreenApi\RestApi\GreenApiClient;

define( "ID_INSTANCE", "1101123456" );
define( "API_TOKEN_INSTANCE", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" );

$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE, 'http://127.0.0.1:8080' );

$result = $greenApi->sending->uploadFile(
    'C:\Games\PicFromDisk.png'
);

print_r(  $result->data );

$result = $greenApi->sending->sendFileByUrl(
    '79876543210@c.us',
    $result->data->urlFile,
    'googlelogo_color_272x92dp.png',
    'Google logo'
);

print_r(  $result->data );