Country Codes has a very simple to use JSON API.
curl "http://countrycodes.theroadtojoy.at/api.php?query=<query>"