Page tree
Skip to end of metadata
Go to start of metadata

In this tutorial, I will tell you how you can use Spreadshirt API v1 to upload designs. You can later use these designs to create new products. I will first tell you about the preliminaries that need to be met in order to be able to upload designs. I will then tell you about the steps that need to be conducted to upload the designs. And finally, I will show you in detail on code samples what exactly you need to do.

Preliminaries

In order to be able to upload designs, the following preconditions need to be met:

  1. You need to own an API key.
  2. The API key needs to be configured for design upload usage.
  3. You have pixel designs to upload (pngs with transparency set).

Steps

To upload a design, you need to conduct four steps in general:

  1. Create the design data by sending a design XML file to the data API.
  2. Fetch the XML data from the API to retrieve the upload URL for the image API.
  3. Extract the upload URL from the XML data.
  4. Upload the pixel design (png) to the image API using the upload URL.

Make sure that you sign the API calls correctly with your API key and secret, when creating the design data and uploading the design (see design resource documentation, security documentation).

Create the Design

To create the design, you need to conduct a HTTP POST call on http://api.spreadshirt.net/api/v1/shops/40000/designs for example.

Payload could be:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<design xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net">
        <name>Super cool design</name>
        <description>A super cool design</description>
 </design>

Response contains HTTP Location header:

http://api.spreadshirt.net/api/v1/shops/40000/designs/u100001143

Fetch the XML Data

To fetch the full XML data for the created design, you need to conduct a HTTP GET call on http://api.spreadshirt.net/api/v1/shops/40000/designs/u100001143. This XML data contains for example the upload URL:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<design xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net" weight="0.0" xlink:href="http://api.spreadshirt.net/api/v1/shops/40000/designs/u100001143" id="u100001143">
    <name>Super cool design</name>
    <description>A super cool design</description>
    <user xlink:href="http://api.spreadshirt.net/api/v1/users/37508" id="37508"/>
    <restrictions>
        <fixedColors>false</fixedColors>
        <ownText>true</ownText>

        <obligatory>true</obligatory>
        <movable>true</movable>
        <targetView>0</targetView>
        <minimumScale>100</minimumScale>
        <visible>true</visible>
        <inverted>false</inverted>

    </restrictions>
    <size unit="px">
        <width>1016.0</width>
        <height>895.0</height>
    </size>
    <colors/>
    <printTypes>
        <printType xlink:href="http://api.spreadshirt.net/api/v1/shops/40000/printTypes/1" id="1"/>
        <printType xlink:href="http://api.spreadshirt.net/api/v1/shops/40000/printTypes/19" id="19"/>
        <printType xlink:href="http://api.spreadshirt.net/api/v1/shops/40000/printTypes/17" id="17"/>
        <printType xlink:href="http://api.spreadshirt.net/api/v1/shops/40000/printTypes/10" id="10"/>
        <printType xlink:href="http://api.spreadshirt.net/api/v1/shops/40000/printTypes/21" id="21"/>
    </printTypes>
    <designCategories/>
    <price>
        <vatExcluded>0.00</vatExcluded>

        <vatIncluded>0</vatIncluded>
        <vat>19.00</vat>
        <currency xlink:href="http://api.spreadshirt.net/api/v1/currencies/1" id="1"/>
    </price>
    <resources>
        <resource xlink:href="http://image.spreadshirt.net/image-server/v1/designs/100001143" type="preview" mediaType="png"/>
<!-- upload url !!! -->
        <resource xlink:href="http://image.spreadshirt.net/image-server/v1/designs/100001143" type="montage" mediaType="png"/>
    </resources>
    <created>2010-08-02T08:29:59Z</created>
</design>

Extract the Upload URL

From the fetched XML file, you need to extract the image API URL from design/resources/resource@type=montage. In our example, this is http://image.spreadshirt.net/image-server/v1/designs/100001143.

Upload the Pixel Design

To upload the pixel design, you need to conduct a HTTP PUT call on http://image.spreadshirt.net/image-server/v1/designs/100001143 and upload a PNG design similar to the following one:

In your code, use your shop id to upload designs to your shop!

Code

Java

The sample Java code for uploading a design could look as follows:

 public class DesignUpload {
    private static final Logger log = LoggerFactory.getLogger(DesignUpload.class);

    public static final String API_KEY = "...";
    public static final String SECRET = "...";

    public static final String UPLOAD_URL = "http://api.spreadshirt.net/api/v1/shops/40000/designs";
    public static final String UPLOAD_XML = "./src/main/resources/designupload/design.xml";
    public static final String UPLOAD_IMAGE = "./src/main/resources/designupload/design.png";

    public static void main(String[] args)
            throws Exception {
        HttpUrlConnectionFactory urlConnectionFactory =
                new HttpUrlConnectionFactory(API_KEY, SECRET, null);
        HttpCallCommandFactory commandFactory =
                new HttpCallCommandFactory(urlConnectionFactory);

        // 1. create design data using xml
        HttpCallCommand createCommand =
                commandFactory.createFileHttpCallCommand(UPLOAD_URL, HttpMethod.POST, null, new File(UPLOAD_XML), null);
        createCommand.setApiKeyProtected(true);
        createCommand.execute();
        if (createCommand.getStatus() >= 400) {
            throw new Exception("Could not create design xml!");
        }
        log.info("XML location is: " + createCommand.getUrl());

        // 2. get created design xml
        HttpCallCommand getCommand =
                commandFactory.createPlainHttpCallCommand(createCommand.getLocation(), HttpMethod.GET, null);
        getCommand.execute();
        if (createCommand.getStatus() >= 400) {
            throw new Exception("Could not retrieve design xml from " + createCommand.getLocation() + "!");
        }
        String message = (String) getCommand.getOutput();

        // 3. determine upload location
        String searchString = "resource xlink:href=\"";
        int index = message.indexOf(searchString);
        String uploadUrl = message.substring(index + searchString.length(),
                message.indexOf("\"", index + searchString.length() + 1));
        log.info("Upload location is: " + uploadUrl);

        // 4. upload image
        HttpCallCommand uploadCommand =
                commandFactory.createFileHttpCallCommand(uploadUrl, HttpMethod.PUT, null, new File(UPLOAD_IMAGE), null);
        uploadCommand.setApiKeyProtected(true);
        uploadCommand.execute();
        if (uploadCommand.getStatus() >= 400) {
            throw new Exception("Could not upload design!");
        }
    }
}

You can find the sample code on SourceForge's spreadshirtapps project at https://spreadshirtapps.svn.sourceforge.net/svnroot/spreadshirtapps/java/tutorials.

PHP

<?php
    // The design upload script shows you how to create designs on the Spreadshirt platform
    // and upload the corresponding design data using Spreadshirt data and image API.


    // 1. Create design entity via data api
    $url = "http://api.spreadshirt.net/api/v1/shops/40000/designs";
    $header = array();
    $header[] = createSprdAuthHeader("POST", $url);
    $header[] = "Content-Type: application/xml";

    $xml = getFileData("design.xml");

    // Initialize handle and set options
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    $result = curl_exec($ch);
    // Close the handle
    curl_close($ch);

    $dataUrl = parseHttpHeaders($result, "Location");

    echo "Design URL: ".$dataUrl."\n";


    // 2. Fetch design data to retrieve upload url
    $ch = curl_init($dataUrl);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    $result = curl_exec($ch);
    // Close the handle
    curl_close($ch);

    $start = strpos($result, "resource xlink:href=\"")+21;
    $end = strpos($result, "\"", $start);
    $imageUrl = substr($result, $start, $end-$start);

    echo "Image URL: ".$imageUrl."\n";


    // 3. Upload design data via image API
    $imageData = getFileData("design.png");

    $header = array();
    $header[] = createSprdAuthHeader("PUT", $imageUrl);
    $header[] = "Content-Type: image/png";

    $ch = curl_init($imageUrl."?method=PUT");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $imageData);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);


    function createSprdAuthHeader($method, $url) {
        $apiKey = "...";
        $secret = "...";
        $time = time()*1000;

        $data = "$method $url $time";
        $sig = sha1("$data $secret");

        return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
    }

    function parseHttpHeaders( $header, $headername ) {
        $retVal = array();
        $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
        foreach( $fields as $field ) {
            if( preg_match('/('.$headername.'): (.+)/m', $field, $match) ) {
                return $match[2];
            }
        }
        return $retVal;
    }

    function getFileData($file) {
        $fp = fopen($file, "r");
        $data = "";
        while(!feof($fp)) {
            $data .= fgets($fp, 1024);
        }
        fclose($fp);
        return $data;
    }
?>

You can find the sample code on SourceForge's spreadshirtapps project at http://spreadshirtapps.svn.sourceforge.net/viewvc/spreadshirtapps/php/samples/src/.

  • No labels