TradingView Snapshot with REST API (Part2)

CHART-IMG
4 min readMar 4, 2023

--

In this second part of the tutorial, I will show you how to use CHART-IMG REST API Version 2 to capture a snapshot of TradingView chart layout. To follow this tutorial, you should know how to authenticate using CURL with API key and how to obtain TradingView sessionid, as explained in part 1.

TradingView Chart

Every TradingView chart layout has a unique LAYOUT_ID that allows for easy sharing with others. You can create multiple layouts with different indicator setups depending on your subscription plan.

You can find your LAYOUT_ID in the URL (J1cFbGBP), which will differ from this example.

PRIVATE CHART LAYOUT WITH COMMUNITY SCRIPTS

Let’s make this chart layout public by toggling the “Sharing” option on the right side of the menu.

PUBLIC CHART LAYOUT WITH COMMUNITY SCRIPTS

Now that your layout is public, anyone can access it with just LAYOUT_ID. To make it private again, you can toggle off the “Sharing” option on the menu.

Shared Layout

The shared layout allows you to take a snapshot of your chart using LAYOUT_ID, but the request body parameters are limited compared to Advanced Chart. I recommend using the private layout if you require full access to your chart. Refer to the documentation for the list of supported real-time exchanges.

Let’s try a simple POST request using CURL to see if it works. Don’t forget to change placeholders {YOUR_API_KEY} and {LAYOUT_ID} with the actual value.

## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/layout-chart/{LAYOUT_ID} \
-H "x-api-key: {YOUR_API_KEY}" \
-o chart-img-07.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/layout-chart/{LAYOUT_ID} -H "x-api-key: {YOUR_API_KEY}" -o chart-img-07.png

This should download an image file chart-img-07.png that shows the same symbol, interval, and indicators as your chart layout.

CHART-IMG-07.PNG

Private Layout

The private layout is recommended if you need full access to your charts, including the maximum number of active and invite-only indicators, as well as real-time data based on your TradingView subscription. Please note that you will lose access to the extended real-time data provided by default.

You need to include your sessionid and sessionid_signin the request header.

## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/layout-chart/{LAYOUT_ID} \
-H "x-api-key: {YOUR_API_KEY}" \
-H "tradingview-session-id: {YOUR_SESSION_ID}" \
-H "tradingview-session-id-sign: {YOUR_SESSION_ID_SIGN}" \
-o chart-img-09.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/layout-chart/{LAYOUT_ID} -H "x-api-key: {YOUR_API_KEY}" -H "tradingview-session-id: {YOUR_SESSION_ID}" -H "tradingview-session-id-sign: {YOUR_SESSION_ID_SIGN}" -o chart-img-09.png

Override Layout

You can reuse the layout with the same indicators and only change the symbol and interval by specifying these parameters in the payload. The request body content-type must be in a valid JSON format.

{
"symbol": "OTC:GBTC",
"interval": "4h"
}

Using multiple layouts with specific indicators for the same symbol allows for better market oversight without overcrowding a single chart.

CHART-IMG-08.PNG
## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/layout-chart/{LAYOUT_ID} \
-H "x-api-key: {YOUR_API_KEY}" \
-H "content-type: application/json" \
-d "{\"symbol\":\"OTC:GBTC\",\"interval\":\"4h\"}" \
-o chart-img-08.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/layout-chart/{LAYOUT_ID} -H "x-api-key: {YOUR_API_KEY}" -H "content-type: application/json" -d "{\"symbol\":\"OTC:GBTC\",\"interval\":\"4h\"}" -o chart-img-08.png

Conclusion

I’m thrilled to announce that version 2 is now available to the general public. This new version includes several features many users have requested for some time, and I can’t wait for everyone to try it out.

If you have any questions or feedback, please don’t hesitate to share them in the comments section below.

Happy Charting!

CHART-IMG

--

--