Let's start with the exchange rate. Any exchange, be it for stocks, real currencies or decentralized peer-to-peer virtual crypto currencies, naturally generates an exchange rate simply from its normal operation. After all, it allows customers to trade, and the price at which trades were last executed is then called the exchange rate on that exchange. So they don't get that data from anywhere, they generate it (not arbitrarily, of course, but according to how their customers place orders and how trades result from matching orders).
All the other quantities you mentioned (which, if I may, I'd like to explain and correct to difficulty of mining blocks, total number of already mined blocks, reward for mining a new block) are specific to each crypto currency. You have to extract them from a client for the specific crypto currency, and the typical approach is to run such a client and use its (local) API.
Your question is how to write your own API. I suppose that means you want to run a website that offers a public API for querying the kind of data you mentioned. A natural approach would be to simply translate the calls for the data inherent to each currency (difficulty, blocks, rewards in your list) to a client you run for that currency. You may want to cache the results for some brief time if you anticipate high usage of your public API.
The exchange rates on different exchanges are more tricky, because the only authoritative source for an exchange's exchange rate is the exchange itself. You will have to find their API and use that. Also, here it would be a bad idea to just forward queries made to your public API. Instead, you should record the exchange rates and answer queries to your public API from this stored data.
There is a chance that many exchanges can still be accessed using only one kind of API convention. Have a look at what API the site bitcoincharts.com requires exchanges to provide. Presumably at least all the exchanges listed there provide it, and probably accept queries from anybody, not just from bitcoincharts.com.