TUTapi tutorial

A beginner-friendly guide to understanding REST APIs using real examples from our API.

Steps
9
Endpoint
/api/states
Auth
Optional

01What is an API?

An API (Application Programming Interface) is like a menu at a restaurant. The menu provides a list of dishes you can order, along with a description of each dish. When you specify what you'd like to order, the kitchen (the system) prepares it and serves it back to you. In the same way, an API lists operations that developers can use, along with a description of what they do.

Think of it this way: when you use a mobile app, the app connects to the Internet and sends data to a server. The server then retrieves that data, interprets it, performs the necessary actions, and sends it back to your phone. The app then interprets that data and presents you with the information you wanted in a readable way. This is what an API does - it's the messenger that takes your request and tells the system what you want to do, then returns the response back to you.

Our API
This tutorial uses our real API that provides information about cannabis legal status across US states. You'll learn by making actual requests and seeing real responses.

02Understanding HTTP methods

HTTP methods tell the server what action you want to perform. Think of them as verbs that describe what you're trying to do:

GET
Retrieve data (like reading a book)
POST
Create new data (like writing a new entry)
PUT
Update existing data (like editing a document)
DELETE
Remove data (like throwing something away)

Most of our API endpoints use GET because they're retrieving information. Let's see a GET request in action:

Live requestGET

This GET request retrieves information about all US states and their cannabis legal status.

GET/api/states

03Reading responses: status codes

Every API response includes a status code that tells you whether your request was successful or not. These are three-digit numbers that fall into different categories:

200Success - The request was successful400Bad Request - The request was invalid or malformed404Not Found - The requested resource was not found500Internal Server Error - The server encountered an error
2xx (Success)
Your request was successful
4xx (Client Error)
There was something wrong with your request
5xx (Server Error)
The server encountered an error

Let's see different status codes in action:

Success (200)GET

This request succeeds and returns data for California.

GET/api/states?state=ca
Not Found (404)GET

This request fails because 'xx' is not a valid state code.

GET/api/states?state=xx

04Request and response format

APIs communicate using JSON (JavaScript Object Notation), which is a way of formatting data that's easy for both humans and computers to read. JSON uses key-value pairs, similar to a dictionary.

Our API responses follow a consistent structure:

{
  "success": true,
  "data": {
    "code": "CA",
    "name": "California",
    "status": "Legalized"
  },
  "meta": {
    "timestamp": "2024-12-XXT00:00:00.000Z"
  }
}
success
Indicates if the request was successful (true/false)
data
The actual information you requested
meta
Additional metadata about the response (like timestamps)

05Query parameters: filtering data

Query parameters let you customize your API requests. They're added to the URL after a question mark (?). You can add multiple parameters by separating them with ampersands (&).

For example: /api/states?state=ca&format=minimal

Use the builder below to experiment with different parameters:

Build your requestGET
Ignored if a state is selected.
Generated URL
/api/states

06Authentication: API keys

Some APIs require authentication to ensure only authorized users can access them. Our API uses API keys, which are like passwords that identify you. API keys are sent in the request headers.

Why use API keys?

  • Higher rate limits (more requests allowed)
  • Access to premium features
  • Better tracking and analytics

To use an API key, you include it in the request headers like this:

Request header
X-API-Key: your-api-key-here
Note
Our API works without authentication for basic usage, but authenticated requests get higher rate limits (1000 requests/hour vs 60 requests/15 minutes).

07Rate limiting

Rate limiting prevents abuse by limiting how many requests you can make in a certain time period. Think of it like a speed limit on a highway - it keeps everyone safe and ensures fair usage.

Public (no API key)60req / 15 min
Authenticated (with API key)1000req / hour
Over the limit
If you exceed the rate limit, you'll receive a 429 status code. Wait a bit and try again.

Rate limit information is included in the response headers. Check the “Response Headers” section when you make a request to see your current rate limit status.

08CORS: cross-origin resource sharing

CORS (Cross-Origin Resource Sharing) is a security feature that controls which websites can make requests to an API from a browser. It's like a bouncer at a club checking IDs.

Why does it matter? Browsers have a “same-origin policy” that blocks requests from one website to another unless the server explicitly allows it. CORS headers tell the browser which origins are allowed.

Our API includes CORS headers in responses, which you can see when you make a request. Look for headers like:

Access-Control-Allow-Origin
Which origins can make requests
Access-Control-Allow-Methods
Which HTTP methods are allowed
Access-Control-Allow-Headers
Which headers can be sent

Make a request below and expand the “Response Headers” section to see CORS headers in action:

Live requestGET

Make this request and check the Response Headers to see CORS headers.

GET/api/states

09Try it yourself

interactive playground

Now that you understand the basics, try building your own API requests! Use the parameter builder below to experiment with different combinations.

Build your requestGET
Ignored if a state is selected.
Generated URL
/api/states
Next steps

Congratulations! You now understand the basics of REST APIs. Ready to dive deeper?