1. General

1.2 Connecting

Just remember, you will need your token in order to connect. Keep it safe.

.

1.2 General - Connecting

1.2.1 Connecting to the REST API

Using our REST API is super easy to do. Just use your favorite programming language's web request package and the data will be returned via JSON or CSV.

To use the REST API, you must let our server know you have an account. You can do this by passing your .

There are two (2) ways to pass your API token using the REST API.

  1. Pass the token directly within the request URL.
  2. Pass the token in your request headers.

1. Pass the token directly within the request URL.

You can pass the token directly in the request URL by passing the token parameter. For example you would query the https://api.tiingo.com/api/test/ endpoint with the token in URL by adding ?token=. Check out the copy & paste example below.

import requests

headers = {
        'Content-Type': 'application/json'
        }
requestResponse = requests.get("https://api.tiingo.com/api/test?token=Not logged-in or registered. Please login or register to see your API Token",
                                    headers=headers)
print(requestResponse.json())
                        
Response:
{'message': 'You successfully sent a request'}

2. Pass the token in your request headers.

You can also pass the token in the request headers. Note you can do this by passing "Token " + to the "Authorization" header. Check out the copy & paste examples below.

import requests

headers = {
        'Content-Type': 'application/json',
        'Authorization' : 'Token Not logged-in or registered. Please login or register to see your API Token'
        }
requestResponse = requests.get("https://api.tiingo.com/api/test/",
                                    headers=headers)
print(requestResponse.json())
                        
Response:
{'message': 'You successfully sent a request'}
1.2 General - Connecting

1.2.2 Connecting to the Websocket API

Websockets allow for two-way communication, allowing us to data to you as soon as it's available. If you want real-time data, this is the fastest way to get it.

This can seem complicated if it's your first experience with websockets, but don't worry - it's just as easy with a RESTful interface and even more efficient. Websockets are both faster and uses less data than RESTful requests.

The web socket API functions a bit differently as you "subscribe" and "unsubscribe" to data sources. From there, you will receive all updates as soon as they're received without having to make requests.

Additionally, when new data comes in there will be a "messageType" which can be

  • "A" for new data
  • "U" for updating existing data
  • "D" for deleing existing data
  • "I" for informational/meta data
  • "E" for error messages
  • "H" for Heartbeats (can be ignored for most cases)

This lets us pass on notices that data has updated or some data is no longer considered valid Each request made to the websocket server contains a JSON object that follows the format:

Response:
{
    'eventName': 'subscribe',
    'eventData': {
                    'authToken': 'Not logged-in or registered. Please login or register to see your API Token',
                    'service':'test'
                }
}

Notice how we have to pass an authorization token just like a REST request. Also notice the HeartBeat message. This is sent every 30 seconds to keep the connection alive.

Once we get our first data request back on a successful connection, the "data" attribute will contain a subscriptionId. This will be used for managing the connection. For example with IEX data, if we want to add or remove tickers in our subscription, we can send updates using the subscriptionId.

from websocket import create_connection
import simplejson as json
ws = create_connection("wss://api.tiingo.com/test")

subscribe = {
                'eventName':'subscribe',
                'eventData': {
                            'authToken': 'Not logged-in or registered. Please login or register to see your API Token'
                            }
                }

ws.send(json.dumps(subscribe))
while True:
    print(ws.recv())
                        
Response:
#Notice the "HeartBeat" message. This is sent every 30 seconds
{"data": {"subscriptionId": 61}, "response": {"message": "Success", "code": 200}, "messageType": "I"}
{"response": {"message": "HeartBeat", "code": 200}, "messageType": "H"}