5.1 What is an API?
So far your app only knows what you type into it. Real prices live on Sectors’ computers. Your app gets them through an API (application programming interface): a set way for one program to ask another for data.
Think of a restaurant. You do not walk into the kitchen; you give your order to a waiter, who brings the food back. The API is the waiter: your app hands it an order, “the company report for BBCA, please”, and it brings back a plate of data. The data arrives as JSON, plain text made of labelled values, such as "last_close_price": 4940.
You will not write this request yourself. The starter kit’s sectors.py is a helper: a small file of ready-made functions that place the order for you.
5.2 API keys and secrets
The waiter only serves members. An API key is your membership card: a long secret code that tells Sectors the request is from you. Each request uses credits, so a key is worth money.
A secret is any value that must not be shared. Your key never goes into app.py, into a Copilot chat, or onto GitHub, where bots scan for keys within minutes. It goes in a separate file called .env, which stays on your laptop.
5.3 Put your key in .env
Do it
In the Explorer, right-click .env.example, choose Copy, then Paste, and rename the copy to .env (exactly that: dot, e, n, v).
Open .env and replace the placeholder with your key, with no spaces and no quotes: SECTORS_API_KEY=your-key-here. Save the file.
That is all. sectors.py reads the key from this file, and in Module 8 you check that Git never uploads it.
5.4 Get to know the helper
Do it in a New Chat in Ask mode, send:
#sectors.py Explain in plain words what each function in this file does, what I give it and what it gives back. Don’t change anything.
one line for each of the three functions:
| Function | You give it | It gives back |
|---|---|---|
get_company_overview |
A ticker, such as "BBCA" |
Basic facts about the company: name, sector, last close price, market cap and more |
get_daily_prices |
A ticker, and optionally a start and end date | A table with one row per trading day: date, open, high, low, close, volume, market cap |
get_company_financials |
A ticker | The company’s yearly financials (used in 7.2) |
Copilot should also mention that the key is read from SECTORS_API_KEY, and that when something goes wrong, such as an unknown ticker, the helper raises a SectorsError: a problem report with a plain message that your app can show on the page.
5.5 Add live prices to your calculator
You are not starting a new app: you add real data to the one you have. Your calculator gets a ticker box, the company’s latest price, and a new answer: how many lots of this stock your monthly saving can buy. On IDX, shares are bought in lots of 100.
Plan.Do it in the same Ask chat, send:
#app.py #sectors.py I want to connect my Investment Goal Calculator to real IDX prices. Frontend: above the calculator, add a text box for an IDX ticker, default BBCA. Show the company name as a heading, then its sector, sub-sector and last close price in rupiah. Under the calculator’s result, add: “Your monthly saving buys X lots of this stock”, plus what is left over. Add a small expandable section at the bottom showing the raw data, so I can see every field. Backend: 1 lot is 100 shares. Lots = monthly saving divided by (last close price × 100), rounded down. Data: use only get_company_overview from sectors.py; if it raises a SectorsError, show its message with st.warning. Don’t write new API code and never put the key in app.py. Tell me the plan in short steps. Don’t write code yet.
Implement. If the plan matches, switch to Agent and send: Build exactly the plan above. Change only app.py. Click Keep.
Test. Click Rerun (or start the app again with streamlit run app.py).
- PT Bank Central Asia Tbk. as a heading; sector Financials, sub-sector Banks; a last close in rupiah
- The calculator still works, and the 0% check from 4.4 still gives Rp 120.000.000
- The lots by hand: if the last close is Rp 4.940, one lot costs 4.940 × 100 = Rp 494.000, so Rp 1.000.000 buys 2 lots with Rp 12.000 left over. Use the price your app shows.
- Typing
TLKMupdates everything; no key appears anywhere inapp.py
Repeat. One improvement of your own, round the same loop: for example, show the lots result only when the monthly saving is at least one lot.
5.6 What does each field mean?
You do not need a finance background; ask. Do it open the raw data section on your page and copy it. In Ask mode, send:
I’m not from finance. Below is data about one Indonesian stock. For each field, explain in one plain sentence what it means and what unit it’s in. Then tell me which three fields matter most for a simple stock page. (paste the data here)
| Field | Plain meaning | Watch out |
|---|---|---|
company_name |
The company’s registered name | |
sector, sub_sector |
The business area: for example Financials, then Banks | |
last_close_price |
The price of one share when the market last closed, in rupiah | Shares trade in lots of 100 |
latest_close_date |
The trading day that price comes from | The market is closed at weekends |
daily_close_change |
How much the closing price moved from the day before | A fraction: −0.0169 means −1.69% |
market_cap |
Market capitalisation: share price × all shares, the market’s price tag for the whole company, in rupiah | A 15-digit number: divide by 10¹² for trillions |
listing_date |
The day the company joined the stock exchange | |
close, volume (daily prices) |
One day’s closing price, and how many shares changed hands |
the explanation should say that daily_close_change is a fraction, not a percent, and that market_cap is in rupiah. Check one number yourself: if market_cap is 602888959530000, that is about Rp 602.9 trillion. These numbers describe the market; they are not a recommendation to buy or sell.