# Photopea API

Photopea API Blog | Learn | Tutorials | API | Facebook | Twitter

# Passing data to Photopea

Photopea can be configured using a parameter after a hash sign.

    https://www.photopea.com#STRING_VALUE

String value is encoded into the URL using classic encoding of query parameters (space as %20 etc.). It corresponds to encodeURI() in Javascript or urlencode() in PHP. This string contains a JSON object.

### JSON configuration object

JSON object must have the following structure:

    {
    	"files" : [
    		"https://www.mysite.com/images/design.psd",
    		"https://www.mysite.com/images/button.png",
    		"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAA\
    		AAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
    	],
    	"resources" : [
    		"https://www.xyz.com/brushes/Nature.ABR",
    		"https://www.xyz.com/grads/Gradients.GRD",
    		"https://www.xyz.com/fonts/NewFont.otf"
    	],
    	"server" : {
    		"url" : "https://www.myserver.com/saveImage.php",
    		"formats" : [ "psd:true", "png", "jpg:0.5" ]
    	},
    	"environment" : {...},
    	"script" : "app.activeDocument.resizeCanvas(90,80,AnchorPosition.TOPLEFT);"
    }

Data URIs can be used - file can be passed inside request (test). All parameters are optional.

### Parameters

- `files` - array of files, that are loaded when Photopea starts
- `resources` - array of resources (gradients, brushes, fonts ...)
- `server` - parameters for saving documents back to a server
  - `url` - URL address of a server
  - `formats` - formats, in which a document should be sent to a server. The string format corresponds to saveToOE.
- `environment` - parameters of the environment, see Environment
- `script` - the script, that should be executed after loading each file (can be long)

## Saving to server

When `server` parameter is specified in a request to Photopea.com, every document opened in Photopea will have `File - Save` option. After a user clicks it, document data are sent to your server in HTTP request using POST method. It will contain a single string parameter `p`. The string contains a JSON object with the following structure:

    {
    	"source" : "https://www.mysite.com/images/button.png",
    	"versions" : [
    		{"format":"psd", "data": "..."},
    		{"format":"jpg", "data": "..."},
    		...
    	]
    }

### Parameters

- `source` - if the file was loaded from your server, the value is the URL of this document. Otherwise (opening a local file, creating an empty file), it contains `"local,X,NAME"`, where X is the integer ID of the document, and NAME is the name of the document
- `versions` different versions of your document
  - `format` - format of the file, exported from Photopea
  - `data` - binary data, encoded into a string using Base64 encoding

Here is a short PHP example, which accepts files from Photopea.

    $p = json_decode( $_POST["p"] );	// parse JSON
    // getting file name from "source";
    $fname = substr ($p->source, strrpos($p->source,"/")+1);
    file_put_contents("images/".$fname, base64_decode( $p->versions[0]->data ));

## Cross-Origin Resource Sharing

For security reasons, webapps can access only files from the same domain. In order to let Photopea load your file, the response of your server must contain the following header:

    Access-Control-Allow-Origin: *

Find out more at CORS specification or at enable-cors.org.

## Prices

Usage of Photopea API is completely free. Keep in mind, that PP is in early stages of development and there may be critical bugs. We do not take any responsibility for documents edited or generated by Photopea.

Please, let us know in advance (support@photopea.com), when you expect to have a large regular traffic (thousands of visitors per day).