Keep Your API Key Secret

Be careful not to include your API key when sharing code. A simple way to ensure this doesn’t happen by accident is to store your API key as an environment variable that can be specified in your .bash_profile or .zshrc file:


This can then be retrieved in your scripts using os.environ:

>>> api_key = os.environ['CHEMSPIDER_API_KEY']
>>> cs = ChemSpider(api_key)

Specify a User Agent

As well as using your API key, it is possible to identify your program to the ChemSpider servers using a User Agent string.

You can specify a custom User Agent through ChemSpiPy through the optional user_agent parameter to the ChemSpider class:

>>> from chemspipy import ChemSpider
>>> cs = ChemSpider('<YOUR-API-KEY>', user_agent='My program 1.3, ChemSpiPy 2.0.0, Python 3.6')


ChemSpiPy can generate logging statements if required. Just set the desired logging level:

import logging

The logger is named ‘chemspipy’. There is more information on logging in the Python logging documentation.