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.
This Ruby example uses the HTTP gem which provides a more convenient API than the standard library packages.
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.
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.
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.
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 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.
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.
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;
namespace paperplane
{
class Example
{
class Job
{
[JsonPropertyName("url")]
public string Url { get; set; }
// ... add further properties here as required.
}
static async Task Main()
{
try
{
// Grab the API key from the environment.
string apiKey = Environment.GetEnvironmentVariable("PAPERPLANE_API_KEY");
if (apiKey is null)
{
Console.WriteLine("API key not set");
return;
}
// Create a value which will be used for HTTP basic authentication.
var auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(apiKey + ":"));
// Set up a new HTTP client.
var http = new HttpClient();
http.BaseAddress = new Uri("https://api.paperplane.app");
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
// Create a JSON string describing a new job.
var job = new Job { Url = "https://example.com" };
var json = JsonSerializer.Serialize(job);
// Post to the jobs endpoint.
var request = new HttpRequestMessage(HttpMethod.Post, "/jobs");
request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var response = await http.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response status code: {0}", response.StatusCode);
Console.WriteLine(body);
}
catch (HttpRequestException e)
{
Console.WriteLine("Exception: {0} ", e.Message);
}
}
}
}