Code samples
Code samples to get you started quickly with Ruby, Python, PHP and Node.js.
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
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
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())
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;
?>
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();
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.package com.mycompany.app;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class App {
public static void main(String[] args) {
try {
// Construct the auth header.
String apiKey = System.getenv("PAPERPLANE_API_KEY");
String auth = apiKey + ":";
byte[] authEncoded = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(authEncoded);
HttpPost request = new HttpPost("https://api.paperplane.app/jobs");
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
// Create form data to specify our URL and set some options.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("url", "https://en.wikipedia.org/wiki/Airplane"));
params.add(new BasicNameValuePair("page_size", "A4"));
request.setEntity(new UrlEncodedFormEntity(params));
// Make the POST request.
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity, "UTF-8");
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(body);
} catch (Exception e) {
System.out.println(e);
}
}
}
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;