AstroPrint 3D Print API

The 3D Print API allows developers to inject STL files into the AstroPrint users' accounts with a few lines of javascript.

The objects that can be imported are: STLs files or ZIP files (The importer will look inside the zip, extract and import each stl file it founds)

This page is in itself an example of all the possible uses. You can see the source code on GitHub.

Get Started

Add the following line close to the closing body tag.

<body>
  ...
  <script type="text/javascript" src="https://cloud.astroprint.com/js/min/lib/astroprint.import.js"></script>
</body>

Sample uses

There are a few ways of using this api.

Option 1: Using HTML Attributes

You can add the class 'astroprint-import' and the following HTML attributes to any html tag and clicking on it will trigger the upload.

Attribute Description Mandatory
data-ap-download-url This is the URL where the STL or ZIP can be found. It needs to be accesible from the internet. Yes
data-ap-name This is the name that will be used to save the file in the user's account. It must have the STL or ZIP extensions. Yes
data-ap-token This token will be passed to the download URL. It can be used to authenticate requests for private models. No
data-ap-protection Wheter the design can be downloaded by user or not. Present if desired, omit if no protection required. No

An example of such usage is:

<button class="astroprint-import" data-ap-download-url="file_url" data-ap-name="test.stl" data-ap-protection="1"> Print with AstroPrint </button>

Click below to see a demo of such usage

Option 2: Calling the Javascript API with a download URL

You can also the Javascript API in response to an event. In this case we use a download URL. The function is importDesign(download_url, name, token) and has the following parameters

Parameter Description Mandatory
download_url This is the URL where the STL or ZIP can be found. It needs to be accesible from the internet. Yes
name This is the name that will be used to save the file in the user's account. It must have the STL or ZIP extensions. Yes
token This token will be passed to the download URL. It can be used to authenticate requests for private models. No
protection Wheter the design can be downloaded by user or not. No

An example of this is as follows. A button that calls the API on the click event.

<button onclick="import()"> Print with AstroPrint </button>
<script>
  function import() {
    astroprint.importDesign("file_url", "test.stl", null, true);
  }
</script>

Click below to see a demo of such usage

Option 3: Calling the Javascript API with a Blob

You can call the Javascript API in response to an event. In this case we use a blob. This is useful when a javascript app generates the data, like for example a browser-based 3D editor. The function is importDesignByBlob(blob, content_type, name) and has the following parameters:

Parameter Description Mandatory
blob This is a blob containing the STL or ZIP data. Yes
content_type This is the content type (mine type) of the content of the blob. 'application/sla' for STL or 'application/zip' for zip files. Yes
name This is the name that will be used to save the file in the user's account. It must have the STL or ZIP extensions. Yes
protection Wheter the design can be downloaded by user or not. No

An example of this is as follows. A button that calls the API on the click event.

<button onclick="import()"> Print with AstroPrint </button>
<script>
  function import() {
    var blob = getDataAsBlob(); //This function is implemented by you to retrieve the STL data as blob
    astroprint.importDesignByBlob(blob, "application/sla", "test.stl", false);
  }
</script>

The example below uses a file input control which returns the local file picked as a blob. This file is then send to the server using the importDesignByBlob API