Skip to main content

Integrating Any API

This guide covers how to integrate any REST API with your Nativeline app. Use this for services not specifically covered elsewhere.

Understanding REST APIs

Most APIs follow REST conventions:
Your App  →  HTTP Request  →  API Server
              (GET, POST)         ↓
Your App  ←  JSON Response  ←  API Server

Finding APIs

Common Categories

  • Communication: Twilio (SMS), SendGrid (Email)
  • Social: Twitter, Instagram, TikTok
  • Data: News APIs, Stock APIs
  • Utilities: URL shorteners, QR codes
  • AI: Stability AI, Replicate

Typical Integration Steps

1

Find the API

Identify the service that provides what you need
2

Read documentation

Understand endpoints, parameters, and authentication
3

Get credentials

Sign up and get your API key or tokens
4

Configure in Nativeline

Tell the AI about the API and provide credentials
5

Make requests

Build the features that use the API

API Authentication Types

API Key (Header)

Most common. Include key in request header:
Add [Service] API with key in Authorization header:
Key: your-api-key-here

API Key (Query Parameter)

Some APIs want the key in the URL:
Add [Service] API with key as query parameter:
Key: your-api-key-here

Bearer Token

Common for OAuth and modern APIs:
Add [Service] API with Bearer token:
Token: your-token-here

OAuth 2.0

For user-authorized access (e.g., accessing user’s social media):
Add OAuth authentication for [Service]:
- Client ID: xxxxx
- Client Secret: xxxxx
- Redirect URL: your-app://callback

Making API Requests

GET Request

Retrieve data:
Fetch the list of items from [API]:
Endpoint: GET https://api.service.com/items

POST Request

Send data:
Send user data to [API]:
Endpoint: POST https://api.service.com/users
Body: { name, email }

With Parameters

Search [API] with query parameters:
Endpoint: GET https://api.service.com/search?q={query}&limit=10

Handling Responses

JSON Parsing

Most APIs return JSON. Tell Nativeline the structure:
The API returns:
{
  "results": [
    { "id": 1, "name": "Item 1" },
    { "id": 2, "name": "Item 2" }
  ],
  "total": 100
}

Parse this and display the results in a list.

Creating Models

Create a model for the API response:
- Item has: id (Int), name (String), description (String?)
- Response has: results (array of Items), total (Int)

Error Handling

Common HTTP Status Codes

CodeMeaningHandle
200SuccessProcess data
400Bad requestCheck your parameters
401UnauthorizedCheck API key
403ForbiddenCheck permissions
404Not foundResource doesn’t exist
429Rate limitedWait and retry
500Server errorRetry later

Implementing Error Handling

Handle API errors:
- 401: Show "Invalid API key"
- 404: Show "Not found"
- 429: Wait 30 seconds and retry
- 500: Show "Service unavailable, try later"

Rate Limiting

Most APIs limit requests:

Understanding Limits

  • Requests per minute/hour/day
  • Check headers: X-RateLimit-Remaining

Handling Rate Limits

If rate limited:
- Show "Please wait" message
- Wait for limit reset
- Use exponential backoff
- Cache responses to reduce calls

Caching

Reduce API calls by caching:
Cache API responses:
- Store response with timestamp
- Use cached data if less than 5 minutes old
- Refresh in background
- Show cached data immediately while loading fresh data

Security Best Practices

Keys should be stored securely, not in source code or logs.
All API calls should use HTTPS to encrypt data in transit.
Don’t assume API responses are always correct or safe.
For sensitive APIs, route calls through your server to hide keys.

Example: News API

Integrate NewsAPI.org:

1. API Key: your-news-api-key
2. Endpoint: GET https://newsapi.org/v2/top-headlines
3. Parameters: country=us, category=technology

Display:
- List of articles
- Each shows: title, source, thumbnail
- Tap to open in Safari

Example: Giphy API

Integrate Giphy:

1. API Key: your-giphy-key
2. Endpoint: GET https://api.giphy.com/v1/gifs/search
3. Parameters: q={searchTerm}, limit=20

Display:
- Grid of GIF thumbnails
- Tap to view full GIF
- Share button

Example: Currency Conversion

Integrate ExchangeRate-API:

1. API Key: your-key
2. Endpoint: GET https://v6.exchangerate-api.com/v6/{key}/latest/USD

Build:
- Currency converter
- Select from/to currencies
- Enter amount
- Show converted result
- Cache rates for 1 hour

Testing APIs

Postman or Similar

Test APIs outside your app first:
  1. Download Postman
  2. Test endpoints manually
  3. Verify responses
  4. Then integrate into app

Nativeline Testing

Add a test screen that:
- Makes a test API call
- Logs the response
- Shows success/failure

When APIs Don’t Work

Debugging Steps

  1. Check API key is valid
  2. Verify endpoint URL
  3. Check request format
  4. Look at error response
  5. Test in Postman
  6. Check API documentation
  7. Contact API support

Common Issues

IssueSolution
CORS errorsUsually not an issue for iOS apps
Invalid JSONCheck API response format
TimeoutIncrease timeout, check network
Wrong dataVerify endpoint and parameters