Skip to content

How to create an API key

PDFCompose.com provides a number of services that you can invoke with a REST API. To use the API, you would need an API key (or token). This article shows you how to generate an API key for your account.

To generate an API key you need to have registered for an account using your email address. To do that, go here and sign up for an account. Once the account set-up is complete, you can generate an API key.

Security Measures

Please take appropriate measures to store the API key securely and do not expose it. Anyone with your key can invoke services on PDFCompose.com on your behalf. Specifically, do not check in the API key into your version control system (such as Git).

Creating the API Key

Once you have logged in, check the page header. You should see an Account dropdown. Click the drop down and you should see a Show API Key menu item.

Once you click that menu item, a dialog box pops up showing your API key. Copy the token and store it in a secure location.

Using the API Key

The following example shows how you can use curl to POST a JSON file to a PDF generation API endpoint.

The arguments are

  • -H "Content-type ... allows you to specify that the posted content is JSON.
  • -d @cert.json instructs curl to POST the contents of the file cert.json.
  • -H "Authorization ... is where you specify your API key (in place of the xxx)
  • -o sample.pdf tells curl to save the output from the API call to the file sample.pdf
  • The last argument is the API endpoint. The endpoint shown in this example generates a PDF certificate using the values in cert.json as shown here.
curl -v -H "Content-type: application/json" \
        -d @cert.json \
        -H "Authorization: Token xxx" \
        -o sample.pdf \
        "https://pdfcompose.com/api/generate-pdf/imenspij/"

The following python example shows how to perform a HTTP post using the API key to generate and fetch the PDF.

We use the python requests module to POST a JSON file to the API endpoint, specifying the authorization token using the header Authorization: Token xxx where xxx is your API token. The response, if successful, is saved to sample.pdf

import requests

url = "https://pdfcompose.com/api/generate-pdf/imenspij/"
resp = requests.post(url,
                     data=open('cert.json'),
                     headers={
                         'Content-type': 'application/json',
                         'Authorization': 'Token xxx
                     })
if resp.status_code < 300:
    with open('sample.pdf', 'wb') as fp:
        fp.write(resp.content)

This example shows to do a POST request in java sending a JSON file to the URL to generate the PDF.

  • Create a HttpURLConnection for the URL.
  • Set request method to POST
  • Set Content-type header as well as the Authorization token.
  • Specify that we want to write data to the output side of the connection.
  • Grab data from the JSON file and write it to the connection.
  • Open the input side of the connection and read and save all the data to the PDF file.
URL url = new URL("https://pdfcompose.com/api/generate-pdf/imenspij/");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "application/json");
con.setRequestProperty("Authorization", "Token xxx");
con.setDoOutput(true);
byte[] bytes = Files.readAllBytes(Paths.get("cert.json"));
try(OutputStream out = con.getOutputStream()) {
    out.write(bytes);
}
try(InputStream in = con.getInputStream();
    FileOutputStream out = new FileOutputStream("sample.pdf")) {
    byte[] ibytes = new byte[2048];
    int len;
    while ((len = in.read(ibytes)) != -1) {
    out.write(ibytes, 0, len);
    }
}

Conclusion

And that was a simple example of how to generate a nice-looking PDF output using a template from PDFCompose.com with your API key. The API key is obtained from My Account section of PDFCompose.com.