Back to Top

 

Getting Started with AlchemyAPI Using Python

Getting Started with AlchemyAPI Using Python

Overview

AlchemyAPI provides advanced text analysis capabilities through a REST API. This allows you to transform your unstructured content from sources like blog posts, news articles, product reviews, forums and social media posts into structured data. With AlchemyAPI, you can easily perform difficult tasks such as extracting keywords, calculating sentiment, or identifying companies.

To use our text analysis functions, AlchemyAPI must be integrated into your application. This tutorial will walk you through the steps to get up and running with AlchemyAPI using the Python programming language. At the conclusion of this tutorial, you will be ready to integrate AlchemyAPI into your application to solve your text analysis needs.

Approximate time required: 5 minutes

Prerequisites

To follow along with this tutorial, you will need the following:

  • Some basic programming skills in Python
  • A basic understanding of how web API's work
  • Python (This tutorial uses v2.7.5+, but any recent version should work, including v3+)
  • Git (to clone the SDK. Alternatively, you can download the SDK as a .zip file if you don't have Git)

Operating System: This tutorial will be created using Ubuntu v13.10 64-bit, but you should be able to complete the same steps using the Mac OSX terminal or the Windows cmd window.

1) Get Your Free API Key

AlchemyAPI requires an API Key to access our text analysis functions. This key will be included into each API request, but don't worry, the SDK will handle this part for you. If you already have an API key, great! Move on to the next step. If not, please click the button to the right to register and receive your API via e-mail.

Note: AlchemyAPI is a paid service that charges per API transaction. It's free to try, and you'll get 1,000 transactions per day on the starter plan. Paid plans start at 5,000 daily transactions and go up to 200M+. Check out the pricing page for more information.

2) Clone Python SDK from GitHub

The AlchemyAPI Python SDK Repo on GitHub

The AlchemyAPI Python SDK Repo at http://github.com/alchemyapi/alchemyapi_python

To clone the SDK to your computer, open up a terminal window and type the following commands:


mkdir -p ~/src/test
cd ~/src/test
git clone https://github.com/AlchemyAPI/alchemyapi_python.git
cd alchemyapi_python/

Note: this is assuming that you'll use ~/src/test as your software development directory. If you want to use a different directory, just navigate to that directory instead before cloning the SDK.

If everything worked, you should see the following output in your terminal window:

Cloning the AlchemyAPI Python SDK to your local machine

Cloning the AlchemyAPI Python SDK repo to your machine

If you don't have Git: If the Git version control system is not available on your machine, you can download a .zip file of the Python SDK from GitHub instead. Just go to: http://github.com/alchemyapi/alchemyapi_python and click the "Download ZIP" button on the right sidebar. Extract the files in the .zip to your software development directory, and then continue on with the tutorial.

3) Configure the Python SDK to use your API Key

Now that you have the Python SDK on your computer, all you need to do is configure it to use your API key. In the alchemyapi_python directory, run:


python alchemyapi.py YOUR_API_KEY

Where YOUR_API_KEY is the 40 character API key you received in your e-mail when you registered. If everything goes okay, you should see the following output:

COnfiguring the Python SDK to use your API Key

Configuring the AlchemyAPI Python SDK to use your API Key

4) Run the Example

You're almost done! The Python SDK comes with an example that calls each of AlchemyAPI's text analysis functions with example data to show how to call each function, and how to parse the output. To run the example, simply type:


python example.py

You should get lots of output in the terminal window as each function is called and the output is parsed, similar to this:

Output from the example in the Python SDK

Example output from running the AlchemyAPI Python SDK example

Next Steps

Congratulations, you've now used all of AlchemyAPI's text analysis functions! However, all you've done is analyze some demo content, and it would be much more interesting to analyze some of your own content. To do that, you can use the example.py file as a guide. Just copy and paste the alchemyapi.py file into your project, and in the file that will handle the text analysis include the following lines:


from alchemyapi import AlchemyAPI
alchemyapi = AlchemyAPI()

Now you can use the alchemyapi object to access any of the text analysis functions, just like the example.py file does. For example, to calculate the sentiment for a simple sentence, try:


myText = "I'm excited to get started with AlchemyAPI!"
response = alchemyapi.sentiment("text", myText)
print "Sentiment: ", response["docSentiment"]["type"]

How you actually integrate AlchemyAPI into your project is outside of the scope of this tutorial, but you should now have the starting point of a proof-of-concept project. If you have any questions or comments, or if you are having trouble getting started with AlchemyAPI using Python, please contact support.

How did we do with this tutorial? Let us know on Twitter: @AlchemyAPI