Code samples

Last updated 3 months ago

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.

require "uri"
require "net/http"
endpoint = URI.parse("https://api.paperplane.app/jobs")
http = Net::HTTP.new(endpoint.host, endpoint.port)
http.use_ssl = true
request = Net::HTTP::Post.new(endpoint.request_uri)
request.basic_auth(ENV.fetch("PAPERPLANE_API_KEY"), "")
request.set_form_data(
"url" => "https://en.wikipedia.org/wiki/Airplane",
"page_size" => "A4"
)
response = http.request(request)
puts response.body

Ruby (using the HTTP gem)

This Ruby example uses the HTTP gem which provides a more convenient API than the standard library packages.

# gem install http
require "http"
response = HTTP.basic_auth(user: ENV.fetch("PAPERPLANE_API_KEY"), pass: "").post(
"https://api.paperplane.app/jobs",
json: { url: "https://en.wikipedia.org/wiki/Airplane", page_size: "A4" }
)
puts response.body

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.

# pip install requests
import os
import requests
response = requests.post(
'https://api.paperplane.app/jobs',
auth=(os.getenv('PAPERPLANE_API_KEY'), ''),
data={'url': 'https://en.wikipedia.org/wiki/Airplane', 'page_size': 'A4'}
)
print(response.json())

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.

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.paperplane.app/jobs',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'url' => 'https://en.wikipedia.org/wiki/Airplane',
'page_size' => 'A4'
),
CURLOPT_USERNAME => getenv('PAPERPLANE_API_KEY')
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>

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.

// npm install node-fetch
const fetch = require("node-fetch");
const url = "https://api.paperplane.app/jobs";
async function createPdf() {
const auth = new Buffer(process.env.PAPERPLANE_API_KEY + ":").toString(
"base64"
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${auth}`
},
body: JSON.stringify({
url: "https://en.wikipedia.org/wiki/Airplane",
page_size: "A4"
})
});
const json = await response.json();
console.log(json);
};
createPdf();

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.