Code samples
Code samples to get you started quickly with Ruby, Python, PHP and Node.js.

Ruby (using standard library)

This Ruby example creates creates a new PDF job. It reads the API key from the PAPERPLANE_API_KEY environment variable, so make sure you have that configured.
1
require "uri"
2
require "net/http"
3
4
endpoint = URI.parse("https://api.paperplane.app/jobs")
5
http = Net::HTTP.new(endpoint.host, endpoint.port)
6
http.use_ssl = true
7
8
request = Net::HTTP::Post.new(endpoint.request_uri)
9
request.basic_auth(ENV.fetch("PAPERPLANE_API_KEY"), "")
10
request.set_form_data(
11
"url" => "https://en.wikipedia.org/wiki/Airplane",
12
"page_size" => "A4"
13
)
14
15
response = http.request(request)
16
puts response.body
Copied!

Ruby (using the HTTP gem)

This Ruby example uses the HTTP gem which provides a more convenient API than the standard library packages.
1
# gem install http
2
3
require "http"
4
5
response = HTTP.basic_auth(user: ENV.fetch("PAPERPLANE_API_KEY"), pass: "").post(
6
"https://api.paperplane.app/jobs",
7
json: { url: "https://en.wikipedia.org/wiki/Airplane", page_size: "A4" }
8
)
9
10
puts response.body
Copied!

Python

This Python example creates creates a new PDF job. It reads the API key from the PAPERPLANE_API_KEY environment variable, so make sure you have that configured.
The requests library is used for making HTTP requests, so you'll need to have that installed.
1
# pip install requests
2
3
import os
4
import requests
5
6
response = requests.post(
7
'https://api.paperplane.app/jobs',
8
auth=(os.getenv('PAPERPLANE_API_KEY'), ''),
9
data={'url': 'https://en.wikipedia.org/wiki/Airplane', 'page_size': 'A4'}
10
)
11
12
print(response.json())
Copied!

PHP

This example creates creates a new PDF job. It reads the API key from the PAPERPLANE_API_KEY environment variable, so make sure you have that configured.
1
<?php
2
3
$curl = curl_init();
4
5
curl_setopt_array($curl, array(
6
CURLOPT_URL => 'https://api.paperplane.app/jobs',
7
CURLOPT_RETURNTRANSFER => true,
8
CURLOPT_POST => true,
9
CURLOPT_POSTFIELDS => array(
10
'url' => 'https://en.wikipedia.org/wiki/Airplane',
11
'page_size' => 'A4'
12
),
13
CURLOPT_USERNAME => getenv('PAPERPLANE_API_KEY')
14
));
15
16
$response = curl_exec($curl);
17
18
curl_close($curl);
19
20
echo $response;
21
22
?>
Copied!

Node.js

This snippet creates creates a new PDF job. It reads the API key from the PAPERPLANE_API_KEY environment variable, so make sure you have that configured.
This example uses the node-fetch library for making HTTP requests.
1
// npm install node-fetch
2
3
const fetch = require("node-fetch");
4
5
const url = "https://api.paperplane.app/jobs";
6
7
async function createPdf() {
8
const auth = new Buffer(process.env.PAPERPLANE_API_KEY + ":").toString(
9
"base64"
10
);
11
const response = await fetch(url, {
12
method: "POST",
13
headers: {
14
"Content-Type": "application/json",
15
Authorization: `Basic ${auth}`
16
},
17
body: JSON.stringify({
18
url: "https://en.wikipedia.org/wiki/Airplane",
19
page_size: "A4"
20
})
21
});
22
const json = await response.json();
23
console.log(json);
24
};
25
26
createPdf();
Copied!

Java

This snippet creates creates a new PDF job. It reads the API key from the PAPERPLANE_API_KEY environment variable, so make sure you have that configured.
This example uses the Apache HttpComponents library for making HTTP requests.
1
package com.mycompany.app;
2
3
import java.nio.charset.StandardCharsets;
4
import java.util.ArrayList;
5
import java.util.List;
6
7
import org.apache.commons.codec.binary.Base64;
8
import org.apache.http.HttpEntity;
9
import org.apache.http.HttpHeaders;
10
import org.apache.http.HttpResponse;
11
import org.apache.http.NameValuePair;
12
import org.apache.http.client.HttpClient;
13
import org.apache.http.client.entity.UrlEncodedFormEntity;
14
import org.apache.http.client.methods.HttpPost;
15
import org.apache.http.impl.client.HttpClientBuilder;
16
import org.apache.http.message.BasicNameValuePair;
17
import org.apache.http.util.EntityUtils;
18
19
public class App {
20
public static void main(String[] args) {
21
try {
22
// Construct the auth header.
23
String apiKey = System.getenv("PAPERPLANE_API_KEY");
24
String auth = apiKey + ":";
25
byte[] authEncoded = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
26
String authHeader = "Basic " + new String(authEncoded);
27
HttpPost request = new HttpPost("https://api.paperplane.app/jobs");
28
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
29
30
// Create form data to specify our URL and set some options.
31
List<NameValuePair> params = new ArrayList<NameValuePair>();
32
params.add(new BasicNameValuePair("url", "https://en.wikipedia.org/wiki/Airplane"));
33
params.add(new BasicNameValuePair("page_size", "A4"));
34
request.setEntity(new UrlEncodedFormEntity(params));
35
36
// Make the POST request.
37
HttpClient httpClient = HttpClientBuilder.create().build();
38
HttpResponse response = httpClient.execute(request);
39
HttpEntity entity = response.getEntity();
40
String body = EntityUtils.toString(entity, "UTF-8");
41
System.out.println(response.getStatusLine().getStatusCode());
42
System.out.println(body);
43
} catch (Exception e) {
44
System.out.println(e);
45
}
46
}
47
}
Copied!

C# (.NET Core)

1
using System;
2
using System.Net.Http;
3
using System.Net.Http.Headers;
4
using System.Text.Json;
5
using System.Text.Json.Serialization;
6
using System.Threading.Tasks;
7
8
namespace paperplane
9
{
10
class Example
11
{
12
class Job
13
{
14
[JsonPropertyName("url")]
15
public string Url { get; set; }
16
17
// ... add further properties here as required.
18
}
19
static async Task Main()
20
{
21
try
22
{
23
// Grab the API key from the environment.
24
string apiKey = Environment.GetEnvironmentVariable("PAPERPLANE_API_KEY");
25
if (apiKey is null)
26
{
27
Console.WriteLine("API key not set");
28
return;
29
}
30
31
// Create a value which will be used for HTTP basic authentication.
32
var auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(apiKey + ":"));
33
34
// Set up a new HTTP client.
35
var http = new HttpClient();
36
http.BaseAddress = new Uri("https://api.paperplane.app");
37
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
38
39
// Create a JSON string describing a new job.
40
var job = new Job { Url = "https://example.com" };
41
var json = JsonSerializer.Serialize(job);
42
43
// Post to the jobs endpoint.
44
var request = new HttpRequestMessage(HttpMethod.Post, "/jobs");
45
request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
46
var response = await http.SendAsync(request);
47
var body = await response.Content.ReadAsStringAsync();
48
Console.WriteLine("Response status code: {0}", response.StatusCode);
49
Console.WriteLine(body);
50
}
51
catch (HttpRequestException e)
52
{
53
Console.WriteLine("Exception: {0} ", e.Message);
54
}
55
}
56
}
57
}
Copied!

Go

This snippet creates creates a new PDF job. It reads the API key from the PAPERPLANE_API_KEY environment variable, so make sure you have that configured.
1
package main
2
3
import (
4
"fmt"
5
"io/ioutil"
6
"log"
7
"net/http"
8
"net/url"
9
"os"
10
"strings"
11
)
12
13
func main() {
14
apiKey := os.Getenv("PAPERPLANE_API_KEY")
15
endpoint := "https://api.paperplane.app/jobs"
16
formData := url.Values{
17
"url": {"https://en.wikipedia.org/wiki/Airplane"},
18
"page_size": {"A4"},
19
}.Encode()
20
21
// Construct the request
22
req, err := http.NewRequest("POST", endpoint, strings.NewReader(formData))
23
if err != nil {
24
log.Fatal(err)
25
}
26
req.SetBasicAuth(apiKey, "")
27
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
28
29
// Send the request
30
client := http.Client{}
31
resp, err := client.Do(req)
32
if err != nil {
33
log.Fatal(err)
34
}
35
fmt.Println("response status:", resp.Status)
36
body, err := ioutil.ReadAll(resp.Body)
37
if err != nil {
38
return
39
}
40
fmt.Println(string(body))
41
}
Copied!

Other languages

We will gradually be adding examples for other languages here. If there’s a specific language you’d like an example for, please get in touch and let us know.
Last modified 1yr ago