The IMSS (Instituto Mexicano del Seguro Social) portal is one of the most frequently automated government websites in Mexico. HR platforms, payroll systems, and compliance tools all need to query it programmatically — but the portal blocks automation with a custom CAPTCHA on every request. This guide shows you how to solve it automatically.
What is the IMSS CAPTCHA?
The IMSS portal at imss.gob.mx uses a distorted image CAPTCHA similar to SAT's — typically 5-6 alphanumeric characters on a noisy background. It appears on:
- Semanas cotizadas lookup (contribution weeks)
- Vigencia de derechos (benefits status)
- Movimientos afiliatorios (affiliation movements)
- Constancia de semanas cotizadas download
- NSS (Número de Seguridad Social) queries
The CAPTCHA uses the same visual distortion pattern as SAT but with a different character set. Standard OCR achieves less than 30% accuracy — you need a model specifically trained on IMSS.
Solving IMSS CAPTCHA with Ocilar
Ocilar has a dedicated AI model trained on IMSS portal CAPTCHAs. Accuracy: 96%+. Average latency: <80ms.
cURL
curl -X POST https://api.ocilar.com/api/v1/solve/imss \
-H "X-API-Key: sk-your_key" \
-H "Content-Type: application/json" \
-d '{"image":"<base64_string>"}'
# Response
{"text":"A7B3X9","confidence":0.97,"latency_ms":74,"credits_used":1} Python — with Playwright
import asyncio
import base64
from playwright.async_api import async_playwright
from ocilar import OcilarClient
client = OcilarClient(api_key="sk-your_key")
async def query_semanas_cotizadas(nss: str, curp: str):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
await page.goto("https://serviciosdigitales.imss.gob.mx/gestionAsegurados-web-externo/asegurado")
# Wait for CAPTCHA to load
await page.wait_for_selector("img[src*='captcha']", timeout=10000)
# Extract CAPTCHA as base64
captcha_b64 = await page.$eval("img[src*='captcha']", """img => {
const canvas = document.createElement('canvas');
canvas.width = img.naturalWidth || img.width;
canvas.height = img.naturalHeight || img.height;
canvas.getContext('2d').drawImage(img, 0, 0);
return canvas.toDataURL('image/png').split(',')[1];
}""")
# Solve with Ocilar
result = client.solve_imss(image_base64=captcha_b64)
print(f"CAPTCHA: {result.text} (confidence: {result.confidence})")
# Fill the form
await page.fill("#nss", nss)
await page.fill("#curp", curp)
await page.fill("#captchaInput", result.text)
await page.click("#btnConsultar")
await page.wait_for_load_state("networkidle")
content = await page.content()
await browser.close()
return content
asyncio.run(query_semanas_cotizadas("12345678901", "JUAJ850101HMCRRL09")) Node.js — with Playwright
import { chromium } from 'playwright'
import { OcilarClient } from '@ocilar/sdk'
const client = new OcilarClient({ apiKey: 'sk-your_key' })
const browser = await chromium.launch({ headless: true })
const page = await browser.newPage()
await page.goto('https://serviciosdigitales.imss.gob.mx/gestionAsegurados-web-externo/asegurado')
await page.waitForSelector("img[src*='captcha']")
const captchaB64 = await page.$eval("img[src*='captcha']", img => {
const canvas = document.createElement('canvas')
canvas.width = img.naturalWidth || img.width
canvas.height = img.naturalHeight || img.height
canvas.getContext('2d').drawImage(img, 0, 0)
return canvas.toDataURL('image/png').split(',')[1]
})
const { text } = await client.solveImss({ image: captchaB64 })
console.log('CAPTCHA solved:', text)
await page.fill('#nss', '12345678901')
await page.fill('#curp', 'JUAJ850101HMCRRL09')
await page.fill('#captchaInput', text)
await page.click('#btnConsultar')
await page.waitForLoadState('networkidle')
await browser.close() Common IMSS portal automation workflows
Semanas cotizadas (contribution weeks)
Verifying how many weeks an employee has contributed to IMSS is required for INFONAVIT loans, pension calculations, and AFORE transitions. HR platforms automate this to provide real-time data without requiring employees to log in manually.
Vigencia de derechos (medical benefits status)
Before processing medical claims or insurance, many platforms verify that the employee's IMSS benefits are active. Automating this check reduces fraud and processing time in health insurance and corporate HR tools.
Movimientos afiliatorios
Tracking affiliation movements (altas, bajas, modificaciones salariales) is critical for payroll compliance. Sync this data automatically into your IMSS-integrated payroll system.
Constancia de semanas cotizadas download
The constancia PDF is required for mortgage applications, pension processes, and benefits claims. Automate its download for multiple employees without manual portal access.
Handling retries
from ocilar import OcilarClient
import time
client = OcilarClient(api_key="sk-your_key")
def solve_imss_with_retry(image_b64: str, max_attempts: int = 3) -> str:
for attempt in range(max_attempts):
result = client.solve_imss(image_base64=image_b64)
if result.confidence >= 0.85:
return result.text
print(f"Low confidence {result.confidence}, retrying...")
time.sleep(1)
return result.text # Return best attempt Pricing
IMSS CAPTCHA solving costs $0.10 per 1,000 solves — same as SAT. Failed solves (under 4%) are not charged.
| Service | IMSS Mexico support | Price / 1K | Accuracy |
|---|---|---|---|
| 2Captcha | Generic image only | $0.50 | ~70% |
| CapSolver | Not supported | — | — |
| Anti-Captcha | Not supported | — | — |
| Ocilar | Dedicated IMSS model | $0.10 | 96%+ |
FAQ
Does this work for all IMSS portal pages?
Yes. The IMSS CAPTCHA type is consistent across the serviciosdigitales.imss.gob.mx subdomain. The same endpoint handles all portal pages that require it.
What accuracy can I expect?
96%+ on IMSS CAPTCHAs. Build retry logic for the ~4% of cases that fail — the second attempt almost always succeeds.
Can I also use this for SAT portal automation?
Yes. See our SAT captcha solver guide for the SAT-specific endpoint and examples.
Is automating IMSS portal access legal?
Ocilar solves the CAPTCHA. Using IMSS's portal programmatically to access your own or your employees' information is standard practice for HR and payroll software. Ocilar is not affiliated with or endorsed by IMSS.