3

Here is a link to their API documentation

I would like to practice web programming by creating a bitcoin price ticker from scratch. My plan is to serve a script that makes api calls to exchanges to display the data. This will mean I only have to serve the script, not handle the data server-side.

I know that part of programming is learning from documentation, but the docs from bitfinex are very sparse and I couldn't find a tutorial.

I created an index.html to test my javascript. It returns a console error:

XMLHttpRequest cannot load https://api.bitfinex.com/v1/pubticker/:last_price. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access. 

Here is the full index.html:

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
</head>

<body>
<script>
$.getJSON("https://api.bitfinex.com/v1/pubticker/:last_price",
    function(data, status){
      alert("price: "+data +" status: " + status);
    }
)
</script>
Thank you stack exchange
</body>
1
  • Your question appears to be one of implementation rather than design (the 'how do I debug this'). As such it doesn't really fit into P.SE well (please see the help center for more about the questions that are within P.SE's scope).
    – user289086
    Commented Jul 22, 2014 at 21:40

2 Answers 2

1

You can't — at least, not with Javascript. That API is not configured to allow calls from Javascript running on other web sites. You will need to call this API from a script running on your web server.

(Also, for what it's worth, the :symbol token in the URL is supposed to be replaced with the symbol of the ticker you're trying to look up, e.g. /v1/pubticker/BTCUSD for BTC/USD exchange prices.)

0

If you run a webserver with SSL on it (as Duskwuff said, "You will need to call this API from a script running on your web server.") then you can deploy the following file to it:

<?php
$func = $_GET['fn'];
if(in_array($func,array('getBfx')))
{
    $func();
}

function getBfx()
{
    $a = $_GET['api'];
    echo "objData = ".file_get_contents($a);
}
?>

Then you can include the script from the URL to which you deployed it, with ?func=getBfx&api=https://api.bitfinex.com/v1{Whatever} tacked onto the end of it. I didn't have to escape that URL, but you might.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.