Getting started overview

This guide provides a rapid path for developers to begin using the positionstack API for geocoding and reverse geocoding tasks. The process involves creating an account, retrieving an API access key, and executing a foundational request to the API's endpoints. With these steps, developers can integrate location data functionalities into their applications using the positionstack service.

positionstack is designed for straightforward integration, offering a RESTful API that returns JSON responses. The service provides specific endpoints for converting human-readable addresses into geographic coordinates (forward geocoding) and for converting geographic coordinates back into human-readable addresses (reverse geocoding) positionstack API documentation.

The following table outlines the key steps to get started with positionstack:

Step What to Do Where
1. Account Creation Register for a free positionstack account. positionstack homepage
2. API Key Retrieval Locate your unique API access_key in the dashboard. positionstack dashboard
3. First Request Construct and execute a basic API call using your access_key. Using cURL, a browser, or a programming language
4. Review Response Examine the JSON output for expected geocoded data. API response body

Create an account and get keys

To access the positionstack API, you must first register for an account. This process establishes your user profile and grants you access to a unique API key, which is necessary for authenticating your requests. positionstack offers a free tier that includes 10,000 requests per month, suitable for initial development and testing positionstack pricing plans.

  1. Navigate to the positionstack website: Open your web browser and go to the official positionstack homepage.

  2. Sign up: Look for a "Sign Up" or "Get API Key" button, typically located in the header or a prominent section of the page. Click on it to initiate the registration process.

  3. Provide registration details: You will likely be prompted to enter an email address and create a password. Some services may also offer signup via social accounts (e.g., Google, GitHub). Complete the required fields.

  4. Verify your email (if required): After submitting your details, check your inbox for a verification email from positionstack. Click the link in the email to confirm your account creation and activate your account.

  5. Access your dashboard: Once verified, log in to your positionstack account. You will be redirected to your personal dashboard.

  6. Locate your API Key: On your dashboard, your unique access_key will be prominently displayed. It might be labeled as "API Key," "Access Key," or similar. This key is a long string of alphanumeric characters. Copy this key, as you will need it for every API request.

    Security Note: Your API key acts as a secret credential. Do not embed it directly in client-side code that can be publicly accessed. For server-side applications, use environment variables or a secure configuration management system to store and retrieve your API key. Exposing your API key can lead to unauthorized usage and depletion of your request quota.

Your first request

With your API key in hand, you can now make your first request to the positionstack API. This example demonstrates a forward geocoding request, converting an address into coordinates. The positionstack API is a RESTful service, meaning you interact with it using standard HTTP methods (primarily GET) and receive JSON responses HTTP/1.1 Semantics and Content.

API Endpoint Structure

The base URL for the positionstack API is http://api.positionstack.com/v1/. For forward geocoding, you will use the /forward endpoint. All requests require your access_key as a URL query parameter.

Forward Geocoding Example (cURL)

The following cURL command demonstrates a basic request to geocode the address "1600 Amphitheatre Parkway, Mountain View, CA". Replace YOUR_ACCESS_KEY with the actual key copied from your positionstack dashboard.


curl 'http://api.positionstack.com/v1/forward'
  --data-urlencode 'access_key=YOUR_ACCESS_KEY'
  --data-urlencode 'query=1600 Amphitheatre Parkway, Mountain View, CA'
    

Explanation of parameters:

  • access_key: Your unique API key for authentication.
  • query: The address string you wish to geocode.

Expected Response

A successful request will return a JSON object containing an array of data, where each element represents a potential geocoded result. The primary fields you will typically look for are latitude, longitude, and label.


{
  "data": [
    {
      "latitude": 37.4219999,
      "longitude": -122.0840575,
      "label": "1600 Amphitheatre Parkway, Mountain View, CA, USA",
      "name": "1600 Amphitheatre Pkwy",
      "type": "address",
      "number": "1600",
      "street": "Amphitheatre Pkwy",
      "postal_code": "94043",
      "confidence": 1,
      "region": "California",
      "region_code": "CA",
      "county": "Santa Clara County",
      "locality": "Mountain View",
      ""administrative_area": null,
      "neighbourhood": "North Bayshore",
      "country": "United States",
      "country_code": "US",
      "map_url": "https://maps.google.com/maps?q=37.4219999,-122.0840575&z=17&hl=en"
    }
  ]
}
    

Reverse Geocoding Example (cURL)

To perform reverse geocoding, you use the /reverse endpoint and provide latitude and longitude parameters.


curl 'http://api.positionstack.com/v1/reverse'
  --data-urlencode 'access_key=YOUR_ACCESS_KEY'
  --data-urlencode 'query=37.4219999,-122.0840575'
    

The response structure for reverse geocoding is similar to forward geocoding, providing an array of results that correspond to the given coordinates.

Common next steps

After successfully making your first requests, consider these common next steps to further integrate positionstack into your application:

  • Explore additional parameters: The positionstack API offers various optional parameters to refine your geocoding results, such as limit (to control the number of results), country (to filter by country), and output (to specify JSON or XML, though JSON is default and recommended). Refer to the positionstack API documentation for a complete list of parameters and their usage.

  • Implement error handling: Integrate robust error handling into your application to gracefully manage API responses that indicate issues, such as invalid API keys, rate limit exceeded, or malformed requests. The API returns HTTP status codes and detailed error messages in the JSON response body.

  • Integrate into your preferred language: While cURL is useful for testing, you'll likely want to integrate positionstack using an HTTP client library in your chosen programming language (e.g., Python's requests, JavaScript's fetch or axios, PHP's Guzzle). The positionstack documentation provides code examples for several popular languages.

  • Manage API key securely: Ensure your API key is stored and accessed securely. For server-side applications, use environment variables to prevent accidental exposure in source code. For client-side web applications, consider proxying requests through your own backend to keep the API key server-side.

  • Monitor usage and upgrade: Keep an eye on your API request usage through your positionstack dashboard. If your application's needs exceed the free tier or your current plan, consider upgrading to a suitable paid plan to avoid service interruptions positionstack pricing plans.

  • Understand rate limits: Be aware of the rate limits associated with your positionstack plan. Implement strategies like request queuing or exponential backoff to handle rate limit errors gracefully and prevent your application from being temporarily blocked Google Maps Platform API key best practices.

Troubleshooting the first call

Encountering issues during your first API call is a common part of the integration process. Here are some typical problems and their solutions when working with positionstack:

  • Invalid API Key (HTTP 401 Unauthorized):

    • Problem: The API returns a 401 status code or an error message indicating an invalid access_key.
    • Solution: Double-check that you have copied the access_key exactly as it appears in your positionstack dashboard. Ensure there are no leading or trailing spaces, and that it's correctly passed as the access_key query parameter.
  • Missing Query Parameter (HTTP 400 Bad Request):

    • Problem: The API responds with a 400 status code, indicating a required parameter is missing.
    • Solution: For forward geocoding, ensure the query parameter is present. For reverse geocoding, ensure both latitude and longitude (or a combined query string like 'lat,lon') are provided. Consult the positionstack API documentation for specific endpoint requirements.
  • No Results Returned (Empty data Array):

    • Problem: The API returns a 200 OK status, but the data array in the JSON response is empty.
    • Solution: This usually means the API could not find a match for your query. Try simplifying the address, checking for typos, or providing more specific details. For reverse geocoding, verify the coordinates are valid and correspond to a known location.
  • Rate Limit Exceeded (HTTP 429 Too Many Requests):

    • Problem: The API returns a 429 status code, indicating you have sent too many requests in a given timeframe.
    • Solution: Wait for the rate limit period to reset before sending more requests. Review your usage on the positionstack dashboard. If this is a recurring issue, consider implementing exponential backoff in your code or upgrading your plan positionstack pricing plans.
  • Network Connectivity Issues:

    • Problem: Your client application receives a network error before getting an API response.
    • Solution: Check your internet connection. Verify that the positionstack API endpoint URL is correct and accessible. Temporarily disable any firewalls or VPNs that might be blocking the connection for testing purposes.
  • Incorrect URL or Endpoint:

    • Problem: You receive a 404 Not Found error.
    • Solution: Confirm that the base URL (http://api.positionstack.com/v1/) and the specific endpoint (/forward or /reverse) are spelled correctly and structured as per the positionstack documentation.