I've added the AlchemyAPI service to my Python app on Bluemix. I can see the API key in the service credentials of the AlchemyAPI service. Where, in the app code or files should I specify this key so that I can call the service? The code runs fine and does everything except the part where I call AlchemyAPI.
I followed the Getting started tutorial here, but it just stops with "Get the key" and doesn't tell me what to do with it.
Some things that I tried but which did not work:
- Added an entry to the
manifest.yml
file, like this. Did not work.
services: - the_alchemy-service_name applications: - path: . env: ALCHEMY_KEY: the_actual_key
- In the app code, called the key before calling AlchemyAPI. Did not work.
VCAP_SERVICES = os.getenv('VCAP_SERVICES') key = (VCAP_SERVICES['alchemy_api'][0]['credentials']['apikey']) from alchemyapi import AlchemyAPI alchemyapi = AlchemyAPI()
The Python API you are using requires the AlchemyAPI key to be passed as arguments to the script or stored in a file. You can see this in the code https://github.com/AlchemyAPI/alchemyapi_python/blob/master/alchemyapi.py
If you want to stick with the AlchemyAPI SDK at https://github.com/AlchemyAPI/alchemyapi_python, it expects the API key to be stored in a file named "api_key.txt" in the current working directory. If you want to use this SDK in Bluemix, and assuming you retrieve the value of the API key from the environment as shown by German, you should create the "api_key.txt" file in your code:
There is a more recent and up-to-date Python SDK available at https://github.com/watson-developer-cloud/python-sdk I would highly recommend using this SDK instead. It supports more features of AlchemyAPI.
Based on the AlchemyAPI you want to use you can look at various examples. Here is one using Alchemy Language: https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/alchemy_language_v1.py
This SDK will automatically find the AlchemyAPI key from the VCAP_SERVICES if you bind the AlchemyAPI service to your application.
Thanks to the resources shared by both @Frederic and @German, I was able to find the answer with a bit more research. I did not use the suggested SDKs as-is because the SDKs contain everything and I am trying to create a simple demo app.
Short answer
Do not call the AlchemyAPI module. Call the Watson Developer Cloud module instead.
Long answer
For a Python app on Bluemix, the dependencies must be listed in a requirements.txt file. Bluemix will automatically pip install these modules, without you having to do anything.
Because I was using the AlchemyAPI service (and went by their Getting Started guide), I listed AlchemyAPI as a dependency in
requirements.txt
. I assumed Bluemix would pip install it. In my Python code, I called that module byfrom alchemyapi import AlchemyAPI
.Wrong assumption.
alchemyapi
cannot be pip installed via Bluemix. The module to call iswatson-developer-cloud
.Once that's called, you can specify the api key so:
So, here is the answer to the question: you use the
api_key
variable to hold the value of the key, and you call thewatson-developer-cloud
module, NOT thealchemyapi
module. You can extract the API key programmatically from the service credentials when you bind the Alchemy service to the app.You can use the
manifest.yml
as long as you don't push your code to a public repository where other people can see your key. Otherwise, I would suggest you to use the Bluemix UI to edit the environment variables.manifest.yml
:Node:
Python:
Java:
You can also bind the Alchemy Service to your Bluemix application and get the key in your environment along with the other environment variables. In this case, the key will be part of the
VCAP_SERVICES
object.The code will be similar in this case but if you use one of the SDKs like the one @Frederic Lavigne mentioned in his answer, the key will be automatically extracted.