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 isJSON
.-d @cert.json
instructscurl
toPOST
the contents of the filecert.json
.-H "Authorization ...
is where you specify your API key (in place of thexxx
)-o sample.pdf
tellscurl
to save the output from the API call to the filesample.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.
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
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 theAuthorization
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.