Author Topic: Buy notes on the secondary market with API  (Read 8408 times)

hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Buy notes on the secondary market with API
« on: November 26, 2017, 10:28:02 AM »
Hi
If I try to buy a NOTE from the SECONDARY market using the PHP script below, I get "Internal Server Error 1510609349610".

I know that My ID and API Key are correct, because I can use them and successfully get the balance and  Notes I currently own, using the same script.
For example here is a note I was trying to buy: https://www.lendingclub.com/foliofn/browseNotesLoanPerf.action?showfoliofn=true&loan_id=80600463&order_id=117950693&note_id=130430010

Any ideas. Please help
Thanks

Code: [Select]
<?php
  $investor_id 
"5166xxxx";
  
$authkey "GVsZuDKATxxxxxxxxxxxxxxxxxx";  

  
/*** set false to receive only datas ***/
  
define("DEBUG_LENDING_API"false);

  
/*** this part for getting balance ***/
  
$balance get_balance($investor_id$authkey);
  
print_r($balance);die;

  
/*** this part for getting notes owned ***/
  
$notes get_notes($investor_id$authkey);
  
print_r($notes);die;

  
/*** this part for buying notes ***/
  
$buy buy_notes($investor_id$authkey);
  
print_r($buy);die;

  function 
get_balance($investor_id$authkey){
    
$balance_url "https://api.lendingclub.com/api/investor/v1/accounts/$investor_id/availablecash";
    
// to get balance call this
     
return call_curl($balance_url$authkey);
  }

  function 
get_notes($investor_id$authkey){
    
$notes_url "https://api.lendingclub.com/api/investor/v1/accounts/$investor_id/notes";
    
// to get notes call this
    
return call_curl($notes_url$authkey);
  }

  function 
buy_notes($investor_id$authkey){
    
$buy_notes_url "https://api.lendingclub.com/api/investor/v1/accounts/$investor_id/trades/buy";
    
// you can add multiple array in notes, mean multiple notes.
    
$note[] = array("loanId" => "80600463""orderId" => "117950693""noteID" => "130430010""bidPrice" => "14.29");
    
//$note[] = array("loanId" => "", "orderId" => "", "noteID" => "", "bidPrice" => "");
    
$datas = array("aid" => "some_id""notes" => $note);
    
$buy_notes call_curl($buy_notes_url$authkeyjson_encode($datas));
    
$notes json_decode($notes['data']);
    return 
$notes;
  }

  function 
call_curl($url$authkey$post "0"){
   
$ch curl_init();
   
curl_setopt($chCURLOPT_URL$url);
   
curl_setopt $chCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0" );
   if(
$post != "0"){
     
curl_setopt($ch,CURLOPT_POST1);
     
curl_setopt($ch,CURLOPT_POSTFIELDS$post);
   }
   
curl_setopt $chCURLOPT_AUTOREFERERtrue );
   
curl_setopt $chCURLOPT_FOLLOWLOCATIONtrue );
   
$headers = array();
   
$headers[] = "Authorization: $authkey";
   
curl_setopt($chCURLOPT_HTTPHEADER$headers);
   
$server_output curl_exec ($ch);
   
$info curl_getinfo($ch);
   
curl_close ($ch);
   if(
DEBUG_LENDING_API == true){
     return array(
"data" => $server_output"response" => $info);
   }else{
     return 
json_decode($server_output);
   }
  }
?>


hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #1 on: December 09, 2017, 09:35:51 PM »
Hi,  Can anyone please help?

Fred93

  • Hero Member
  • *****
  • Posts: 2243
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #2 on: December 10, 2017, 01:18:04 AM »
Any time you get a "server error", I would ask LC.  That means something went wrong inside their server, likely something unusual you did that was not checked at the right spot to give you a decent error message.

AnilG

  • Hero Member
  • *****
  • Posts: 1122
    • View Profile
    • PeerCube
Re: Buy notes on the secondary market with API
« Reply #3 on: December 10, 2017, 05:23:07 AM »
Have you been able to successfully make simple Folio API buy call first? The best practice for using API calls is to first make the simplest API call, capture the received errors and responses and then incorporate rest of the logic. Have you captured the complete response received from your curl API buy call? Have you inserted debugging statements to see where your program craps out?
---
Anil Gupta
PeerCube Thoughts blog https://www.peercube.com/blog
PeerCube https://www.peercube.com

Rob L

  • Hero Member
  • *****
  • Posts: 2131
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #4 on: December 10, 2017, 10:46:02 AM »
You might try to talk to the API using the program SoapUI first. It's an excellent tool.
An open source free download is at: https://www.soapui.org/

Here's an example of a Folio API sell in C using libcurl. If I remember (its been a while), the transfer and accept encoding setopts are very important. Probably why I commented those lines. I doubt its changed but this code hasn't been run in over a year. No guarantees, use this code at your own risk.

Code: [Select]
strcpy( api_sell_url, lc_api_url );
strcat( api_sell_url, "/accounts/" );
strcat( api_sell_url, lc_account );
strcat( api_sell_url, "/trades/sell" );    
curl_easy_setopt(curl_handle_lc, CURLOPT_URL, api_sell_url );

strcpy( api_sell_authorization, "Authorization: " );
strcat( api_sell_authorization, lc_api_credentials );

curl_easy_setopt(curl_handle_lc, CURLOPT_VERBOSE, 0L );
curl_easy_setopt(curl_handle_lc, CURLOPT_POST, 1L);
curl_easy_setopt(curl_handle_lc, CURLOPT_TRANSFER_ENCODING, 1L); // ask LC to encode its reply
curl_easy_setopt(curl_handle_lc, CURLOPT_ACCEPT_ENCODING, "" ); // LC POST reply is gzip encoded

headers_lc = curl_slist_append( headers_lc, "User-Agent:libcurl-agent-rcl/1.0");
headers_lc = curl_slist_append( headers_lc, "Accept-Encoding: gzip,deflate" );
headers_lc = curl_slist_append( headers_lc, "Accept: application/json");
headers_lc = curl_slist_append( headers_lc, "Content-Type: application/json");
headers_lc = curl_slist_append( headers_lc, api_sell_authorization );
curl_easy_setopt(curl_handle_lc, CURLOPT_HTTPHEADER, headers_lc );

// assemble json POST string, for example:
// {"aid":12345678,"expireDate":"05/20/2016","notes":[{"loanId":5617733,"orderId":7946887,"noteId":24523159,"askingPrice":21.78}]}

sprintf( api_sell_json_request, "{\"aid\":%s,\"expireDate\":\"%s\",\"notes\":[{\"loanId\":%d,\"orderId\":%d,\"noteId\":%d,\"askingPrice\":%f}]}",
lc_account, expiredate, loanid, orderid, noteid, askprice );
 
// use LC RESTful API POST to place the order
chunk_lc.memory = malloc(1);
chunk_lc.size = 0;

curl_easy_setopt(curl_handle_lc, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle_lc, CURLOPT_WRITEDATA, (void *)&chunk_lc);

curl_easy_setopt(curl_handle_lc, CURLOPT_POSTFIELDS, api_sell_json_request );

res_lc = curl_easy_perform(curl_handle_lc);

if(res_lc != CURLE_OK) {

// a curl error was encounterd, clean up and return error code
strcpy( api_response, "{\"sellNoteStatus\": \"ERROR\",\"sellNoteConfirmations\": [ { \"executionStatus\": [\"CURL_HTTP_POST_FAILED\"] } ] }" );

if(chunk_lc.memory)
free(chunk_lc.memory);

curl_slist_free_all(headers_lc);
headers_lc = NULL;

return( -1 );
}

hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #5 on: December 10, 2017, 10:57:59 AM »
Thanks Anil, Fred.
I am able to get the OWNED NOTES and BALANCE with the script, so i believe the api call is correct
Only wen I execute the BUY part of the script, it gives the "Internal Server Error"
What would the syntax be for a "simple Folio API buy call"? Do you have any examples I can use.
I tried going over the LC docs too.
Unfortunately the script was written by someone else and is not longer available, so I am trying to get it to work.

Thanks
Hil

apc3161

  • Newbie
  • *
  • Posts: 43
    • View Profile
    • Email
Re: Buy notes on the secondary market with API
« Reply #6 on: December 10, 2017, 11:46:35 AM »
You can try emailing LC directly, they've been pretty supportive in my experience.

hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #7 on: December 22, 2017, 12:21:39 PM »
After lots of back and forth with LC support they are now telling me that there is a different Key to buy Notes off the secondary market (other than the Api). They mention that, its the reason why my script (above) is not working. I do not find any key other than my API.
Is that true or are they just yanking my chain?

Fred93

  • Hero Member
  • *****
  • Posts: 2243
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #8 on: December 22, 2017, 02:57:03 PM »
After lots of back and forth with LC support they are now telling me that there is a different Key to buy Notes off the secondary market (other than the Api). They mention that, its the reason why my script (above) is not working. I do not find any key other than my API.
Is that true or are they just yanking my chain?

Same key is used for both APIs.

hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #9 on: December 23, 2017, 09:31:44 AM »
I thought so too. But here is the email I received from support
Quote
I just heard back from one of the managers of the developer team and he informed me that he noticed you are missing the "X-LC-Application-Key" header in addition to the Authorization header. This header is required for API access for the secondary market

Also there is no mention for an authorization header in their api documentation.  https://www.lendingclub.com/foliofn/folioInvestingAPIDocument.action. Are they alternate docs showing us this authorization code that I am missing.

Fred93

  • Hero Member
  • *****
  • Posts: 2243
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #10 on: December 23, 2017, 04:54:13 PM »
I thought so too. But here is the email I received from support
Quote
I just heard back from one of the managers of the developer team and he informed me that he noticed you are missing the "X-LC-Application-Key" header in addition to the Authorization header. This header is required for API access for the secondary market

Also there is no mention for an authorization header in their api documentation.  https://www.lendingclub.com/foliofn/folioInvestingAPIDocument.action. Are they alternate docs showing us this authorization code that I am missing.

Yes, it is mentioned.  I think there used to be more words about this in the LC documentation, but most of them seem to have disappeared.

Look here: https://www.lendingclub.com/foliofn/APIDocumentationHeaders.action

I'm working from memory here...  The idea they had was that there were at one time many people in the business of buying and selling LC notes for other people.  These were web sites you could join and pay a fee for someone to choose loans for you, and buy them for you, etc.  Other people were writing software you could download to your own computer to do the same thing for you.  You can imagine that LC thought they should know not only who the client (account owner) is, but also who the 3rd party who wrote the software or operates the service is.  I don't know their exact reasoning, but suppose there was a technical problem ... they would want to reach out to the operator of the service, not the individual investor.  There might also be legal issues where they would want to ban one of these 3rd party guys, while not banning the individual investors who use him. 

This header was always optional.  If you wrote your own software and used it to invest your own money, you didn't need the header.  If you wrote software to be used to buy and sell for other people, you were supposed to apply for an application key, and stick it in.

I have never used this header.  My software invests my own money.

hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #11 on: December 29, 2017, 11:12:39 AM »
Thanks Fred for the details.

I modified the script with the authorization header and key and I still get the "Internal server error". 
I have debugged the code further and I see the error appears on line $server_output .

Code: [Select]
<?php
  $invester_id 
"516******";
  
$authkey "I18********************=";
  
define("DEBUG_LENDING_API"false);
  
$buy buy_notes($invester_id$authkey);
  
print_r($buy);die;

  function 
buy_notes($invester_id$authkey){
    
$buy_notes_url "https://api.lendingclub.com/api/investor/v1/accounts/$invester_id/trades/buy";
    
$note[] = array("loanId" => "97277470""orderId" => "139320895""noteID" => "149206918""bidPrice" => "19.45");
    
$datas = array("aid" => "some_id""notes" => $note);
    
$buy_notes call_curl($buy_notes_url$authkeyjson_encode($datas));
    
$notes json_decode($notes['data']);
    return 
$notes;
  }
  function 
call_curl($url$authkey$post "0"){
   
$invester_id "516*******";
   
$ContentType "application/json";
   
$ch curl_init();
   
curl_setopt($chCURLOPT_URL$url);
   
curl_setopt $chCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0" );
   if(
$post != "0"){
     
curl_setopt($ch,CURLOPT_POST1);
     
curl_setopt($ch,CURLOPT_POSTFIELDS$post);
   }
   
//curl_setopt ( $ch, CURLOPT_AUTOREFERER, true );
   //curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
   
$headers = array();
   
$headers[] = "Authorization: $authkey";
   
$headers[] = "Content-type: $ContentType";
   
$headers[] = "Accept: $ContentType";
   
$headers[] = "X-LC-Application-Key: $invester_id";
   
curl_setopt($chCURLOPT_HTTPHEADER$headers);
   
curl_setopt($chCURLOPT_RETURNTRANSFER1);
   
curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
   
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
   
$server_output curl_exec ($ch);
   echo 
$server_output"<br>";
   
//exit;
   
$info curl_getinfo($ch);
   
curl_close ($ch);
   if(
DEBUG_LENDING_API == true){
     return array(
"data" => $server_output"response" => $info);
   }else{
     return 
json_decode($server_output);
   }
  }
?>

?>
« Last Edit: December 29, 2017, 11:15:13 AM by hdsouza »

Fred93

  • Hero Member
  • *****
  • Posts: 2243
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #12 on: December 29, 2017, 01:54:46 PM »
Quote
    $datas = array("aid" => "some_id", "notes" => $note);

aid should be set to the account number of the account into which you want to buy the notes.  Perhaps that's $invester_id in your case? 

Then it should say "aid" => $invester_id 

and you should not use the X_LC_Application_Key header unless LC has given you an application key to use for your software. 

hdsouza

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #13 on: December 29, 2017, 03:30:25 PM »
Thanks Fred. That did it!!!!
I finally got a "SUCCESS_PENDING_SETTLEMENT"

Fred93

  • Hero Member
  • *****
  • Posts: 2243
    • View Profile
Re: Buy notes on the secondary market with API
« Reply #14 on: December 29, 2017, 04:15:14 PM »
Now you get to learn the joys of "PENDING SETTLEMENT", and how many don't make it thru this step.