Project 6 — Advanced
REST API + JSON + Database Bot
Modern automation sirf GUI pe nahi chalti — APIs se. Is project me bot banayenge jo CRM ki REST API se leads fetch kare, JSON parse kare, aur SQL Server me insert kare — token auth, rate limiting, retry ke saath. Ye "modern RPA developer" ka signature skill hai.
Project kya karega?
WHAT
Bot pehle OAuth token lega (login API call), phir leads ki paginated API call karega, har lead ka JSON parse karke DataTable banayega, aur SQL Server ke Leads table me upsert karega. Failed records Orchestrator queue me retry ke liye jayenge.
WHEN
System integration jab kare karni ho — CRM se ERP, ERP se warehouse, kisi bhi SaaS tool ka data warehouse me. API-first automation 2026 ki demand hai — sirf GUI bot banane wale replace ho rahe hain.
WHERE
Integration teams, data engineering ke chhote jobs, fintech operations (payment reconciliation APIs), HR tech (attendance API → payroll DB). Har SaaS company ke paas API hoti hai, usse chalne wale bots ki demand high hai.
Step-by-step build
Main.xaml (Sequence)
|
+-- Try Catch
| +-- Sequence: Get Token
| | +-- HTTP Request
| | Url: https://api.crm.com/auth/token
| | Method: POST
| | Body: {"client_id": in_ClientId, "client_secret": in_Secret}
| | ContentType: application/json
| | -> strTokenResp
| | +-- Assign: strToken = Deserialize JSON Property(strTokenResp, "access_token")
| +-- Sequence: Fetch & Load
| | +-- Assign intPage = 1
| | +-- While blnMoreData
| | +-- HTTP Request
| | | Url: "https://api.crm.com/leads?page=" + intPage + "&limit=100"
| | | Method: GET
| | | Headers: Authorization = "Bearer " + strToken
| | | -> strLeadsResp
| | +-- Assign: jArr = Deserialize JSON (strLeadsResp).SelectToken("data")
| | +-- For Each item in jArr
| | | +-- Assign: dtLeads.Rows.Add(item("id"), item("name"), item("email"), item("status"))
| | +-- Assign: blnMoreData = strLeadsResp.Contains("next_page")
| | +-- Assign: intPage = intPage + 1
| | +-- Delay 1000 ms (rate limit respect)
| | +-- For Each Row in dtLeads
| | +-- Try Catch
| | +-- Execute Non Query (SQL):
| | | MERGE INTO Leads AS t
| | | USING (SELECT @id AS Id) AS s ON t.Id = s.Id
| | | WHEN MATCHED THEN UPDATE SET Name=@name, Status=@status
| | | WHEN NOT MATCHED THEN INSERT (Id, Name, Email, Status) VALUES (@id,@name,@email,@status);
| | +-- Set Transaction Status / Write Row: "OK"
| | +-- Catch
| | +-- Add Queue Item queue: "CRM_Lead_Failed" Data: row
| +-- Catch (AuthException)
| +-- Log Message LogLevel.Error "Token expired - refresh flow run"
| +-- (Alert Orchestrator / terminate for retry)
JSON parsing ke 3 tarike
# 1. Deserialize JSON Property - simple flat field:
strToken = Deserialize JSON Property(strResp, "access_token")
# 2. JSON Select Token - nested path (JSONPath):
strCity = JsonSelectToken(strResp, "data[0].address.city")
# 3. For Each (JArray) - array iterate karke DataTable me:
jArr = Deserialize JSON(strResp).SelectToken("data")
For Each item in jArr -> dt.Rows.Add(item("id"), item("name"))
# XPath/JSONPath samjho to 90% API integration easy hai.
Error handling — auth, rate limit, duplicates
- 401 Token expired: Refresh token flow — alag HTTP call se naya token lo, retry. Token expiry 1 hr standard.
- 429 Rate limited: Retry Scope with exponential delay (5s → 10s → 20s). API limits docs me likhi hoti hai.
- 4xx Bad request: Payload check — retry ka fayda nahi. Log + skip (business exception).
- Duplicate insert: MERGE/upsert use karo — idempotent. Dobara run = data dobara na jaaye.
Enterprise tips
- Credentials kabhi code me mat likho — Orchestrator Asset (Credential type) se laao. Audit trail milta hai.
- Connection string bhi Orchestrator Asset me — server change pe bot redeploy nahi.
- Har API call ka request ID / timestamp log karo — debugging me API team saath degi.
- SQL me parameterized queries (Execute Non Query ke arguments) — SQL injection se bachav.
Exercise
Question: JSON response se ek simple field (jaise access_token) nikalne ki activity ka naam? (2-3 words)
Question: API failure pe retry logic wrap karne wali activity? (2 words)
Common mistakes
- Credentials in code: Client secret code me = security review me reject. Orchestrator Asset.
- No pagination loop: API default 20 records deti hai — 500 leads me se 20 hi aaye. next_page check karo.
- Blind INSERT: Dobara run pe duplicate key error. MERGE/upsert use karo.
- Har response pe trust: API field badal sakti hai — null check + default values daalo.
Project ban gaya? Ab next project pe chalte hain — HR Onboarding Multi-App Bot.