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 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.
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.
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.
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.
packagecom.mycompany.app;importjava.nio.charset.StandardCharsets;importjava.util.ArrayList;importjava.util.List;importorg.apache.commons.codec.binary.Base64;importorg.apache.http.HttpEntity;importorg.apache.http.HttpHeaders;importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.client.HttpClient;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;publicclassApp {publicstaticvoidmain(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 "+newString(authEncoded);HttpPost request =newHttpPost("https://api.paperplane.app/jobs");request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);// Create form data to specify our URL and set some options.List<NameValuePair> params =newArrayList<NameValuePair>();params.add(newBasicNameValuePair("url","https://en.wikipedia.org/wiki/Airplane"));params.add(newBasicNameValuePair("page_size","A4"));request.setEntity(newUrlEncodedFormEntity(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); } }}
C# (.NET Core)
usingSystem;usingSystem.Net.Http;usingSystem.Net.Http.Headers;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;usingSystem.Threading.Tasks;namespacepaperplane{classExample {classJob { [JsonPropertyName("url")]publicstring Url { get; set; } // ... add further properties here as required. }staticasyncTaskMain() {try { // Grab the API key from the environment.string apiKey =Environment.GetEnvironmentVariable("PAPERPLANE_API_KEY");if (apiKey isnull) {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 =newHttpClient();http.BaseAddress=newUri("https://api.paperplane.app");http.DefaultRequestHeaders.Authorization=newAuthenticationHeaderValue("Basic", auth); // Create a JSON string describing a new job.var job =newJob { Url ="https://example.com" };var json =JsonSerializer.Serialize(job); // Post to the jobs endpoint.var request =newHttpRequestMessage(HttpMethod.Post,"/jobs");request.Content=newStringContent(json,System.Text.Encoding.UTF8,"application/json");var response =awaithttp.SendAsync(request);var body =awaitresponse.Content.ReadAsStringAsync();Console.WriteLine("Response status code: {0}",response.StatusCode);Console.WriteLine(body); }catch (HttpRequestException e) {Console.WriteLine("Exception: {0} ",e.Message); } } }}
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.
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.