0

Am trying to leverage the Anvil API for PDF.

Here is their sample request.

curl \
  -X POST \
  -u YOUR_API_KEY: \
  -H 'Content-Type: application/json' \
  -d '{ "data": { "someKey": "some data" } }' \
  https://app.useanvil.com/api/v1/fill/{pdfTemplateID}.pdf > test.pdf

My problem is where to add the API KEY. I have tried adding it to the header but it throws error {"name":"AuthorizationError","message":"Not logged in."}

Here is the coding so far

$url2="https://app.useanvil.com/api/v1/fill/first.pdf";
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL, $url2);

$apiKey ='my api key goes here';

$post_data ='
{


  "data": {
    "someName": "Bobby",
    "someDate": "2018-10-31",
    "anAddress": {
      "street1": "123 Main St",
      "city": "San Francisco",
      "state": "CA",
      "zip": "94106"
    }
  }
}';

curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
//'Content-Type:application/json'
'Authorization: ' . $apiKey
));  


curl_setopt($ch2,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch2,CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch2,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch2,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch2,CURLOPT_RETURNTRANSFER, true);
echo $response2 = curl_exec($ch2);

curl_close($ch2);
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38

4 Answers4

1

The curl command you provided has option -u, which is expecting data as username:password ,from curl man

-u/--user user:password Specify user and password to use for server authentication. If this option is used several times, the last one will be used.

which in PHP you have to send headers like below snippet:

CURLOPT_HTTPHEADER => [
        'Authorization: Basic ' . $apiKey . ':'
    ],

or with

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");  

related thread

Edit: from your link in comment, they are expecting raw data of your request which you can accomplish by sending it as put request: curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');

or with text/plain header

user969068
  • 2,818
  • 5
  • 33
  • 64
  • It still throw the same error. Here is the postman documentation API https://documenter.getpostman.com/view/14149649/TVzSjcj6#a86e8972-a2e5-4804-9312-8a2286fa878f – Nancy Moore Feb 17 '21 at 06:50
1

Easiest way to do add api key in curl request is as follow

// Collection object
    $ch = curl_init($url);
    
 $headers = array(
         "APIKEY: PUT_HERE_API_KEY",
         "Content-type: text/xml;charset=\"utf-8\"",
         "Accept: text/xml"
     );
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_TIMEOUT, 60);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
    $result = curl_exec($ch); // execute
    $result;
   //show response
    curl_close($ch);
Hassan Qasim
  • 463
  • 5
  • 5
0
        **
        
         - ***UPDATED***
        
        **
    are you encoding the API key as "base64" ?? 
    
    
        $YOUR_API_KEY =  base64_encode("YOUR_API_KEY");

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://app.useanvil.com/api/v1/fill/XnuTZKVZg1Mljsu999od.pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"title\": \"Hello\", \"data\": [ { \"label\": \"Hello World\", \"content\": \"I like turtles\" } ] }");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $YOUR_API_KEY . ':' . '');

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
  • It still throw the same error. Here is the postman documentation API https://documenter.getpostman.com/view/14149649/TVzSjcj6#a86e8972-a2e5-4804-9312-8a2286fa878f – Nancy Moore Feb 17 '21 at 06:51
-1
curl -X GET -k -H 'Content-Type: application/json' -H 'X-ApiKey : YOUR_APIKEYHERE' -i 'YOUR API RNDPOINT URL HERE'

X-ApiKey is the name of your API key.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • IF Your Getting Auth Error Message Try This -H 'Authorization: bearer YOUR_APITOKEN_VALUE_HERE' – Arjun Kumar Feb 17 '21 at 06:58
  • -H means Header And bearer : Bearer Token A security token with the property that any party in possession of the token (a "bearer") can use the token in any way that any other party in possession of it can. Using a bearer token does not require a bearer to prove possession of cryptographic key material (proof-of-possession). – Arjun Kumar Feb 17 '21 at 07:00