Bird
Raised Fist0
Rest APIprogramming~20 mins

Rate limit headers (X-RateLimit) in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Rate Limit Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this rate limit header parsing code?

Given this HTTP response header string, what will the printed output be?

Rest API
headers = {
    'X-RateLimit-Limit': '1000',
    'X-RateLimit-Remaining': '250',
    'X-RateLimit-Reset': '1672531200'
}

limit = int(headers.get('X-RateLimit-Limit', 0))
remaining = int(headers.get('X-RateLimit-Remaining', 0))
reset = int(headers.get('X-RateLimit-Reset', 0))

print(f"Limit: {limit}, Remaining: {remaining}, Reset: {reset}")
ALimit: 1000, Remaining: 0, Reset: 1672531200
BLimit: 1000, Remaining: 250, Reset: 1672531200
CLimit: 0, Remaining: 0, Reset: 0
DLimit: 1000, Remaining: 250, Reset: 0
Attempts:
2 left
💡 Hint

Check how the get method retrieves values and the default values used.

🧠 Conceptual
intermediate
1:30remaining
What does the X-RateLimit-Reset header represent?

In REST APIs, what does the X-RateLimit-Reset header usually indicate?

AThe time when the rate limit window resets, usually as a UNIX timestamp
BThe total number of allowed requests in the current window
CThe duration in seconds until the rate limit resets
DThe number of requests remaining before the limit is reached
Attempts:
2 left
💡 Hint

Think about what a timestamp represents compared to a duration.

🔧 Debug
advanced
2:00remaining
Why does this code raise a ValueError when parsing rate limit headers?

Consider this code snippet that parses rate limit headers:

limit = int(headers['X-RateLimit-Limit'])
remaining = int(headers['X-RateLimit-Remaining'])
reset = int(headers['X-RateLimit-Reset'])

Sometimes it raises ValueError. Why?

AThe headers dictionary is immutable, so int() cannot convert values
BHeaders might be missing, causing a KeyError, not ValueError
CHeader values might be empty strings or non-numeric, causing int() to fail
DThe int() function cannot convert string numbers to integers
Attempts:
2 left
💡 Hint

Check what happens if the header value is not a valid number string.

📝 Syntax
advanced
2:30remaining
Which option correctly sets rate limit headers in a Flask response?

You want to add rate limit headers to a Flask HTTP response. Which code snippet is correct?

Rest API
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/')
def index():
    response = jsonify(message='Hello')
    # Add headers here
    return response
A
response.headers['X-RateLimit-Limit'] = '500'
response.headers['X-RateLimit-Remaining'] = '100'
response.headers['X-RateLimit-Reset'] = '1672531200'
B
response.set_header('X-RateLimit-Limit', '500')
response.set_header('X-RateLimit-Remaining', '100')
response.set_header('X-RateLimit-Reset', '1672531200')
C
response.headers.add('X-RateLimit-Limit', 500)
response.headers.add('X-RateLimit-Remaining', 100)
response.headers.add('X-RateLimit-Reset', 1672531200)
D
response.headers = {
    'X-RateLimit-Limit': 500,
    'X-RateLimit-Remaining': 100,
    'X-RateLimit-Reset': 1672531200
}
Attempts:
2 left
💡 Hint

Check how to assign headers in Flask response objects.

🚀 Application
expert
3:00remaining
How many requests remain after this sequence of API calls with rate limit headers?

An API returns these headers on each call:

X-RateLimit-Limit: 10
X-RateLimit-Remaining: 7
X-RateLimit-Reset: 1700000000

You make 3 calls in a row, each returning the same headers but with X-RateLimit-Remaining decreasing by 1 each time. What is the X-RateLimit-Remaining value after the third call?

A0
B7
C10
D4
Attempts:
2 left
💡 Hint

Subtract one remaining request per call from the initial remaining count.

Practice

(1/5)
1.

What does the X-RateLimit-Remaining header indicate in a REST API response?

easy
A. The time when the rate limit will reset.
B. The total number of API calls allowed per day.
C. The number of API calls made so far.
D. The number of API calls you can still make before hitting the limit.

Solution

  1. Step 1: Understand the meaning of X-RateLimit-Remaining

    This header shows how many calls you have left before reaching the limit.
  2. Step 2: Compare with other headers

    X-RateLimit-Limit is total allowed calls, X-RateLimit-Reset is reset time, so remaining calls is the count left.
  3. Final Answer:

    The number of API calls you can still make before hitting the limit. -> Option D
  4. Quick Check:

    Remaining calls = calls left [OK]
Hint: Remaining means how many calls you can still make [OK]
Common Mistakes:
  • Confusing remaining with total limit
  • Thinking it shows reset time
  • Assuming it counts calls made
2.

Which of the following is the correct way to read the X-RateLimit-Reset header?

HTTP/1.1 200 OK
X-RateLimit-Reset: 1686000000
easy
A. It is a Unix timestamp indicating when the limit resets.
B. It shows the number of calls left before reset.
C. It is the total allowed calls per hour.
D. It shows the current time in ISO format.

Solution

  1. Step 1: Identify the header type

    X-RateLimit-Reset usually gives a timestamp for when the limit resets.
  2. Step 2: Interpret the value

    The value 1686000000 looks like a Unix timestamp (seconds since 1970).
  3. Final Answer:

    It is a Unix timestamp indicating when the limit resets. -> Option A
  4. Quick Check:

    Reset header = Unix timestamp [OK]
Hint: Reset header is always a timestamp in seconds [OK]
Common Mistakes:
  • Thinking reset shows calls left
  • Confusing reset with total limit
  • Assuming reset is current time
3.

Given the following response headers:

X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 250
X-RateLimit-Reset: 1686003600

How many API calls have been made so far?

medium
A. 750
B. 250
C. 1000
D. 1250

Solution

  1. Step 1: Understand the headers

    Total allowed calls are 1000, remaining calls are 250.
  2. Step 2: Calculate calls made

    Calls made = Total limit - Remaining = 1000 - 250 = 750.
  3. Final Answer:

    750 -> Option A
  4. Quick Check:

    1000 - 250 = 750 calls made [OK]
Hint: Calls made = Limit minus Remaining [OK]
Common Mistakes:
  • Using remaining as calls made
  • Adding limit and remaining
  • Confusing reset time as calls made
4.

You receive these headers from an API:

X-RateLimit-Limit: 500
X-RateLimit-Remaining: -10
X-RateLimit-Reset: 1686007200

What is the likely problem?

medium
A. The headers are missing the total calls made.
B. The limit is too low for the API.
C. The remaining calls cannot be negative; it's an error.
D. The reset time is in the past.

Solution

  1. Step 1: Check the X-RateLimit-Remaining value

    Remaining calls cannot be negative; it should be zero or positive.
  2. Step 2: Identify the error

    A negative remaining value indicates a bug or miscalculation in the API response.
  3. Final Answer:

    The remaining calls cannot be negative; it's an error. -> Option C
  4. Quick Check:

    Remaining calls must be ≥ 0 [OK]
Hint: Remaining calls can never be negative [OK]
Common Mistakes:
  • Ignoring negative values as valid
  • Confusing reset time with remaining
  • Thinking limit is the problem
5.

You want to build a client that stops making API calls when the limit is reached and waits until reset. Given these headers:

X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1686009000

What should your client do?

hard
A. Continue making calls; the limit resets immediately.
B. Stop calls and wait until the reset timestamp before retrying.
C. Ignore the headers and retry after 1 minute.
D. Reset the remaining count manually and continue.

Solution

  1. Step 1: Check remaining calls

    Remaining is 0, so no calls can be made now.
  2. Step 2: Use reset time to wait

    The client should wait until the reset timestamp before making new calls.
  3. Final Answer:

    Stop calls and wait until the reset timestamp before retrying. -> Option B
  4. Quick Check:

    Remaining=0 means wait until reset [OK]
Hint: Stop calls at zero remaining; wait for reset time [OK]
Common Mistakes:
  • Ignoring zero remaining and continuing calls
  • Guessing reset time instead of using header
  • Manually resetting counters in client