Getting started with the Spaces lookup endpoints

This quick start guide will help you make your first request to one of the Spaces lookup endpoints with a set of specified fields using Postman.

If you would like to see sample code in different programming languages, please visit our Twitter API v2 sample code GitHub repository.

Prerequisites

For you to be able to complete this guide, you will have need to have a set of keys and tokens, which you can generate by following these steps:

  • Apply for a developer account and receive approval.
  • Create a Project and an associated developer App in the developer portal.
  • Navigate to your app's “Keys and tokens” page, and save your API Keys, Access Tokens, and Bearer Token to your password manager.

     

Steps to build a Spaces lookup request

For this example, we will make a request to the user Spaces lookup by creator ID endpoint, but you can apply the learnings from this quick start to other lookup requests as well. 

Step one: Start with a tool or library

There are several different tools, code examples, and libraries that you can use to make a request to this endpoint, but we are going to use the Postman tool here to simplify the process.

To load Twitter API v2 Postman collection into your environment, please click on the following button:

 

Once you have the Twitter API v2 collection loaded in Postman, navigate to the Spaces folder and find the "Spaces by creator ID" request.

Step two: Authenticate your request

To properly make a request to the Twitter API, you need to verify that you have permission to do so. To do this with the Spaces lookup endpoints, you must authenticate your request. Using OAuth 2.0 Bearer Token is significantly easier, so we will describe how to use this method in this guide.

Step three: Identify and specify which user from which you would like to retrieve Tweets

You must specify a user you would like to retrieve live or upcoming Spaces for within the request. In this example, we will be passing a single user ID.

User IDs are simply the numerical value that represents an account handle that you can find within an account's profile URL. For example, the following account’s username is TwitterDev.

https://twitter.com/TwitterDev

To convert this username to the user ID, you will have to use the user lookup endpoint with the username and find the numerical user ID in the payload. In the case of @TwitterDev, the user ID is 2244994945.

In Postman, navigate to the "Params" tab and enter this user ID into the "Value" column of the id parameter.

Key Value
id 2244994945

 

Step four: Identify and specify which fields you would like to retrieve

If you click the "Send" button after step three, you will receive an id, which is the only Space object field returned by default in your response.

If you would like to receive additional fields, you will have to specify them in your request with the space.fields or expansions parameters.

For this exercise, we will requested three additional sets of fields from different objects:

  • The additional title field in the primary Spaces object.
  • The full user object of the specified creator ID
  • The additional user.created_at field in the associated user object.

     

In Postman, navigate to the “Params” tab and add the following key:value pair to the “Query Params” table:

 

Key

Value

Returned fields

space.fields

title

creator_id

expansions

creator_id

includes.users.id, includes.users.name, includes.users.username

user.fields

created_at

includes.users.created_at

 

You should now see the following URL next to the “Send” button:

https://api.twitter.com/2/users/:id/spaces?space.fields=creator_id&expansions=creator_id&user.fields=created_at

 

Please note:

In Postman, the path parameter :id in the URL field will not automatically update to the value that you enter into the id params field, which is why the above URL includes :id and not 22449945.

Step five: Make your request and review your response

Once you have everything set up, hit the “Send” button and you will receive the following response:

      {
   "data": [
    {
        "creator_id": "2244994945",
        "id": "1zqKVXPQhvZJB",
        "title": "Hello world 👋",
        "state": "live"
   },
   "includes": {
       "users": [
           {
               "created_at": "2013-12-14T04:35:55.000Z",
               "name": "Twitter Dev",
               "id": "2244994945",
               "username": "TwitterDev"
           }
       ]
   }
]
}

    
Was this document helpful?
Thank you

Thank you for the feedback. We’re really glad we could help!

Thank you for the feedback. How could we improve this document?
Thank you for the feedback. Your comments will help us improve our documents in the future.